﻿//using VT_Tool_DLL;
using IRC_DLL_VTDR6110C;
using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.x_ViewTrix
{
    public partial class VTDR6110C_Gamma : Form
    {

        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        private double[,] D_lums;//目标值-单色
        private double[] D_lum = new double[25];//目标值-白色
        private byte[] Send_buf = new byte[1];
        List<string> lines;
        private byte[] Rec_tmp = new byte[1];
        private byte[] Rec_tmp1 = new byte[1];
        private List<byte> buffer = new List<byte>(4096);//默认分配1/16页内存，并始终限制不允许超过  
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke  
        //创建类型为Person的对象集合
        List<Setting.Gamma_Property> Diven_Gamma = new List<Setting.Gamma_Property>();
        List<Setting.Offset_Property> Diven_Offset = new List<Setting.Offset_Property>();
        //监听NT37701A挂载
        string[] DBV_SET = { "", "" };
        string[] GAMMA_SET = { "", "" };
        private List<string> ImageType;
        private static bool Gamma_adjust_timeout_flag = false;
        private int index = 0;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        public static bool firstGammaFlag = true;
        frmMain frmTop = null;
        public VTDR6110C_Gamma(frmMain WTDR6110C)
        {
            frmTop = WTDR6110C;
            InitializeComponent();
            frmTop.Child_window_hide_already = true;
            frmTop.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体  

            string[] s = { ".bmp" };
            this.ImageType = new List<string>(s);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        #region   //=================================LCD cfg function:OLED_Show_Click====Start=================
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;

            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {
                case "btnPatternW":                             //写入Pattern参数
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;
                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //R
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break; //G
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break; //B
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break; //W
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //BL
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                frmTop.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("OLED_Show串口有误，请检查", "错误");
            }
            //Thread.Sleep(800);
        }
        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox_Gray.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }

        private void checkBox_Gray_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_Gray.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }
        private void checkBox1_Click(object sender, EventArgs e)
        {
            if (ChkCMD3.Checked)
            {
                numPatternG.Enabled = false;
                numPatternB.Enabled = false;
            }
            else
            {
                numPatternG.Enabled = true;
                numPatternB.Enabled = true;
            }
        }
        #endregion //=================================LCD cfg function: OLED_Show_Click====end=================     
        private void VTDR6110C_Gamma_Read_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[157];
            Button btnTmp = (Button)sender;
            Send_buf[0] = 0x6B;
            Send_buf[2] = 0x05; //Length

            ///Gamma set
            if (rbt_VTDR6110C_Gamma1.Checked)
            {
                Send_buf[3] = 0x00;
            }
            else if (rbt_VTDR6110C_Gamma2.Checked)
            {
                Send_buf[3] = 0x01;
            }
            else if (rbt_VTDR6110C_Gamma3.Checked)
            {
                Send_buf[3] = 0x02;
            }
            else if (rbt_VTDR6110C_Gamma4.Checked)
            {
                Send_buf[3] = 0x03;
            }
            else if (rbt_VTDR6110C_Gamma5.Checked)
            {
                Send_buf[3] = 0x04;
            }
            else if (rbt_VTDR6110C_Gamma6.Checked)
            {
                Send_buf[3] = 0x05;
            }
            else if (rbt_VTDR6110C_Gamma7.Checked)
            {
                Send_buf[3] = 0x06;
            }
            else if (rbt_VTDR6110C_Gamma8.Checked)
            {
                Send_buf[3] = 0x07;
            }
            else if (rbt_VTDR6110C_Gamma9.Checked)
            {
                Send_buf[3] = 0x08;
            }
            switch (btnTmp.Name)
            {
                case "VTDR6110C_GammaR":
                    {
                        VTDR6100_0XFE_XX = 0x00;
                        Send_buf[1] = 0x09;
                        Send_buf[4] = 0x96;   //每组gamma数量
                    }
                    break;
                case "VTDR6110C_GammaW":
                    {

                    }
                    break;
                case "VTDR6110C_OTP":
                    {
                        VTDR6100_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0D;
                        // Send_buf[4] = 0x96;   //每组gamma数量
                    }
                    break;

                default:
                    break;
            }
            Setting.TempRecord.cmdLen = Send_buf[2] + 5;
            CRCData.CalculateCrc16Start_Length(Send_buf, 0, Setting.TempRecord.cmdLen - 2, out Send_buf[Setting.TempRecord.cmdLen - 2], out Send_buf[Setting.TempRecord.cmdLen - 1]);
            try
            {
                frmTop.comPort.Write(Send_buf, 0, Setting.TempRecord.cmdLen);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmTop.T1.Enabled = false;
            frmTop.T1.Interval = 2000;
            frmTop.T1.Enabled = true;
        }
        private byte Gammnum;
        private void btnGammaValueUpdate_Click(object sender, EventArgs e)      //刷新UI上表格内的Gamma数据
        {
            if (rbt_VTDR6110C_Gamma1.Checked)
            {
                Gammnum = 0x00;
            }
            else if (rbt_VTDR6110C_Gamma2.Checked)
            {
                Gammnum = 0x01;
            }
            else if (rbt_VTDR6110C_Gamma3.Checked)
            {
                Gammnum = 0x02;
            }
            else if (rbt_VTDR6110C_Gamma4.Checked)
            {
                Gammnum = 0x03;
            }
            else if (rbt_VTDR6110C_Gamma5.Checked)
            {
                Gammnum = 0x04;
            }
            else if (rbt_VTDR6110C_Gamma6.Checked)
            {
                Gammnum = 0x05;
            }
            else if (rbt_VTDR6110C_Gamma7.Checked)
            {
                Gammnum = 0x06;
            }
            else if (rbt_VTDR6110C_Gamma8.Checked)
            {
                Gammnum = 0x07;
            }
            else if (rbt_VTDR6110C_Gamma9.Checked)
            {
                Gammnum = 0x08;
            }


            int Line_Count = 0;
            try
            {
                Line_Count = GammaReg_dataGridView.RowCount;
            }
            catch
            {
                MessageBox.Show("表格数据(行数)异常！", "提示：");
                return;
            }
            try
            {
                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                {
                    GammaValue[i, 0] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[2].Value);
                    GammaValue[i, 1] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[3].Value);
                    GammaValue[i, 2] = Convert.ToUInt32(GammaReg_dataGridView.Rows[i].Cells[4].Value);
                }
                //MessageBox.Show("数据更新OK！", "提示：");
                txttip.Text += "Gamma数据更新OK！\r\n";

                CheckandCorrectGammaValue();   //检查Gamma 是否存在高低灰阶数值错乱 ,并刷新表格数据              
                GammaValueFormat_Click(null, null);  //Gamma 值与实际地址进行转换 
                GammaValueSet(0, 0, 0, 0, 0, 0, GammaValueTeparr); //GammaValue 转换成特定格式后再通过此函数写入DDIC
                btnCalculateVTDR6100.PerformClick();
            }
            catch
            {
                MessageBox.Show("Gamma数据更新错误！", "唐韬说：");
            }
        }
        uint GammaValueMax = 1024 - 1;
        byte GPN = 0; //保存Gamma绑点数量
        private void CheckandCorrectGammaValue()   //检查GammaValue 是否按照大小排列
        {
            byte i; GPN = (byte)Point_VTDR6110C.Length; // 获取Gamma绑点数量
            if (GammaValue[GPN - 1, 0] > GammaValueMax)
            {
                GammaValue[GPN - 1, 0] = GammaValueMax - 16;
            }
            if (GammaValue[GPN - 1, 1] > GammaValueMax)
            {
                GammaValue[GPN - 1, 1] = GammaValueMax - 16;
            }
            if (GammaValue[GPN - 1, 2] > GammaValueMax)
            {
                GammaValue[GPN - 1, 2] = GammaValueMax - 16;
            }  //确保GammaValue不要超过DDIC 中Gamma寄存能接受的最大值


            for (i = (byte)(GPN - 1); i > 0; i--)    //低灰阶GammaValue如果大于高灰阶GammaValue则强制变小
            {
                if (GammaValue[i, 0] < GammaValue[i - 1, 0])
                {
                    GammaValue[i - 1, 0] = GammaValue[i, 0] - 0;
                    //GammaValueFormat_Click(null, null);
                }
                if (GammaValue[i, 1] < GammaValue[i - 1, 1])
                {
                    GammaValue[i - 1, 1] = GammaValue[i, 1] - 0;
                    //GammaValueFormat_Click(null, null);
                }
                if (GammaValue[i, 2] < GammaValue[i - 1, 2])
                {
                    GammaValue[i - 1, 2] = GammaValue[i, 2] - 0;
                    //GammaValueFormat_Click(null, null);
                }
            }
            GammaValue[0, 0] = 0; GammaValue[0, 1] = 0; GammaValue[0, 2] = 0;
            if (chkGammaGridViewFresh.Checked)   // 刷新UI界面GammaValue值显示
            {
                this.GammaReg_dataGridView.Rows.Clear();
                for (i = 0; i < GPN; i++)
                {
                    DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                }
            }
        }
        private void VTDR6110C_Gamma_Button_Click(object sender, EventArgs e)
        {
            this.GammaReg_dataGridView.Rows.Clear();
            Send_buf = new byte[157];
            Button btnTmp = (Button)sender;
            Send_buf[0] = 0x6B;
            Send_buf[2] = 0x05; //Length

            ///Gamma ..select
            if (rbt_VTDR6110C_Gamma1.Checked)
            {
                Gammnum = 0x00;
            }
            else if (rbt_VTDR6110C_Gamma2.Checked)
            {
                Gammnum = 0x01;
            }
            else if (rbt_VTDR6110C_Gamma3.Checked)
            {
                Gammnum = 0x02;
            }
            else if (rbt_VTDR6110C_Gamma4.Checked)
            {
                Gammnum = 0x03;
            }
            else if (rbt_VTDR6110C_Gamma5.Checked)
            {
                Gammnum = 0x04;
            }
            else if (rbt_VTDR6110C_Gamma6.Checked)
            {
                Gammnum = 0x05;
            }
            else if (rbt_VTDR6110C_Gamma7.Checked)
            {
                Gammnum = 0x06;
            }
            else if (rbt_VTDR6110C_Gamma8.Checked)
            {
                Gammnum = 0x07;
            }
            else if (rbt_VTDR6110C_Gamma9.Checked)
            {
                Gammnum = 0x08;
            }
            Send_buf[3] = Gammnum;
            switch (btnTmp.Name)
            {
                case "VTDR6110C_GammaR":
                    {
                        VTDR6100_0XFE_XX = 0x00;
                        Send_buf[1] = 0x09;
                        Send_buf[4] = 0x96;   //每组gamma数量

                    }
                    break;
                case "VTDR6110C_GammaW":
                    {
                        GammaValueFormat_Click(null, null);
                        byte rh = 1;
                        byte rl = 1;
                        byte gh = 1;
                        byte gl = 1;
                        byte bh = 1;
                        byte bl = 1;

                        GammaValueSet(rh, rl, gh, gl, bh, bl, GammaValueTeparr);
                    }
                    break;
                case "VTDR6110C_OTP":
                    {
                        VTDR6100_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0D;
                        // Send_buf[4] = 0x96;   //每组gamma数量
                    }
                    break;

                default:
                    break;
            }
            Setting.TempRecord.cmdLen = Send_buf[2] + 5;
            CRCData.CalculateCrc16Start_Length(Send_buf, 0, Setting.TempRecord.cmdLen - 2, out Send_buf[Setting.TempRecord.cmdLen - 2], out Send_buf[Setting.TempRecord.cmdLen - 1]);
            try
            {
                frmTop.comPort.Write(Send_buf, 0, Setting.TempRecord.cmdLen);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmTop.T1.Enabled = false;
            frmTop.T1.Interval = 2000;
            frmTop.T1.Enabled = true;
        }

        private void VTDR6110C_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            ////frmTop.comPort.Close()
            frmTop.comPort.DataReceived -= new SerialDataReceivedEventHandler(VTDR6110C_DataReceived);
            frmTop.comPort.DataReceived += new SerialDataReceivedEventHandler(frmTop.SerialPort1_DataReceived);
            ////frmTop.comPort.Open();
            frmTop.textBox1.Text = frmTop.textBox1.Text + "VTDR6110C_Gamma_FormClosed！\r\n";
            frmTop.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmTop.Child_window_hide_already = false;
            frmTop.IC_comboBox.Enabled = true;
            firstGammaFlag = true;

        }

        private void VTDR6110C_Gamma_VisibleChanged(object sender, EventArgs e)
        {

            if (frmTop.Child_window_hide_already == false) //子窗体已开启标识
            {
                ////frmTop.comPort.Close()
                frmTop.comPort.DataReceived -= new SerialDataReceivedEventHandler(VTDR6110C_DataReceived);
                frmTop.comPort.DataReceived += new SerialDataReceivedEventHandler(frmTop.SerialPort1_DataReceived);
                ////frmTop.comPort.Open();
                frmTop.textBox1.Text = frmTop.textBox1.Text + "VTDR6110C_Gamma_FormHide！\r\n";
                frmTop.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmTop.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmTop.SerialPort1_DataReceived);
                frmTop.comPort.DataReceived += new SerialDataReceivedEventHandler(VTDR6110C_DataReceived);
                frmTop.textBox1.Text = frmTop.textBox1.Text + "IC69310_Gamma_FormOpened！\r\n";
                frmTop.Child_window_hide_already = false; //子窗体已开启标识
            }

        }

        private float ELVDDC;
        private void VTDR6110C_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing)
            { return; }
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmTop.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;
                Setting.TempRecord.cmdLen = n;
                frmTop.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    //RECEIVE
                    // 0x0A: 模组Timing参数 设置  返回
                    // 0x0D: 模组的SSD2828配置数据、DriverIC初始化数据  设置  返回
                    // 0x2F: Demura操作    设置  返回
                    // 0x99: 通用读写、寄存器读写  设置  返回
                    // 0x9E: 功耗测试单元  设置  返回
                    if ((buffer[0] == 0x99) && (Setting.TempRecord.cmdLen > 0))
                    {

                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);  //正确分析一条数据，从缓存中移除数据。  
                            }
                        }
                    }
                    else if ((buffer[0] == 0x6B) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);
                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);  //正确分析一条数据，从缓存中移除数据。  
                            }
                        }

                    }
                    else if (((buffer[0] == 0x1D) || (buffer[0] == 0x0D) || (buffer[0] == 0x0A) || (buffer[0] == 0x2F) || (buffer[0] == 0x99) || (buffer[0] == 0x9e) || (buffer[0] == 0x9f)) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, buffer.Count);  //正确分析一条数据，从缓存中移除数据。  
                            }
                        }
                    }

                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmTop.T1.Enabled = false; //停止超时计数
                        if (Rec_tmp[0] == 0x6B) //
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        //  if (Rec_tmp[4] == 0x00)
                                        //     frmTop.textBox1.Text = frmTop.textBox1.Text + "visionox 寄存器\t写 OK！\r\n";
                                        reText.Text = String.Format("{0:X2}", Rec_tmp[3]) + "寄存器写OK\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        string tmp = "";                    //8bit  read
                                        reText.Text = "";
                                        {
                                            for (int i = 0; i <= (Rec_tmp[4] + 1); i++)
                                            {
                                                if (i == (Rec_tmp[4] + 1))
                                                {
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i + 3]);//16进制

                                                }
                                                else if (i == 1)
                                                {
                                                    //不显示回读位数
                                                }
                                                else
                                                {
                                                    tmp = tmp + String.Format("{0:X2}", Rec_tmp[i + 3]) + ",";//16进制
                                                }

                                            }
                                        }
                                        // frmTop.Generate_Read_Result_txt.AppendText(tmp + "\r\n");
                                        reText.Text = tmp;
                                        Application.DoEvents();
                                    }
                                    break;
                                case 0x09:                 //read gamma
                                    {
                                        // if (Rec_tmp[4] == 0x00)
                                        frmTop.textBox1.Text = frmTop.textBox1.Text + "Gamma寄存器Read OK！\r\n";
                                        // frmTop.textBox1.Text = frmTop.textBox1.Text + BitConverter.ToString(Rec_tmp);
                                        int i = 5;
                                        //Array.Copy(Rec_tmp, 5, GammaValueReceived, 0, 150);
                                        for (i = 0; i < 25; i++)
                                        {
                                            GammaValue[i, 0] = (uint)(Rec_tmp[i * 2 + 5] << 8) + Rec_tmp[i * 2 + 6];
                                            GammaValue[i, 1] = (uint)(Rec_tmp[i * 2 + 5 + 50] << 8) + Rec_tmp[i * 2 + 6 + 50];
                                            GammaValue[i, 2] = (uint)(Rec_tmp[i * 2 + 5 + 100] << 8) + Rec_tmp[i * 2 + 6 + 100];
                                        }

                                        for (i = 0; i < 25; i++)
                                        {
                                            DataGirdShow((byte)i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                                        }
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        //  if (Rec_tmp[4] == 0x00)
                                        //     frmTop.textBox1.Text = frmTop.textBox1.Text + "visionox 寄存器\t写 OK！\r\n";
                                        reText.Text = "gamma OTP OK\r\n";
                                    }
                                    break;
                                default: break;

                            }
                        }
                        else if (Rec_tmp[0] == 0x9E)  // PowerTest
                        {
                            switch (Rec_tmp[3])
                            {
                                case 0x00:
                                    {
                                        double PMU_ddic_power = 0;
                                        double PMU_panel_power = 0;
                                        double PMU_power = 0;
                                        //VCI = (float)(BitConverter.ToUInt16(Rec_tmp, 12) * 0.00125); if (VCI < 0.05) { VCI = 0; }
                                        //VCIC = (float)(BitConverter.ToUInt16(Rec_tmp, 14) * 2.5) / (float)(262);//250
                                        //VDDIO = (float)(BitConverter.ToUInt16(Rec_tmp, 16) * 0.00125); if (VDDIO < 0.05) { VDDIO = 0; }
                                        // VDDIOC = (float)(BitConverter.ToUInt16(Rec_tmp, 18) * 2.5) / (float)(137);//120
                                        //AVDD = (float)(BitConverter.ToUInt16(Rec_tmp, 8) * 0.00125); if (AVDD < 0.05) { AVDD = 0; }
                                        //AVDDC = (float)(BitConverter.ToUInt16(Rec_tmp, 10) * 2.5) / (float)(120);
                                        //ELVDD = (float)(BitConverter.ToUInt16(Rec_tmp, 4) * 0.00125); if (ELVDD < 0.05) { ELVDD = 0; }
                                        ELVDDC = (float)(BitConverter.ToUInt16(Rec_tmp, 6) * 2.5) / 120;
                                        ELVDDC = ELVDDC - (float)(0.6112 - 0.007674 * ELVDDC);
                                        labELVDDC.Text = ELVDDC + "mA";
                                        //ELVSS = (float)(BitConverter.ToUInt16(Rec_tmp, 28) * 0.00157); if (ELVSS < 0.05) { ELVSS = 0; }
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        //--------------------------------------------Demura----------------------------------------
                        else if (Rec_tmp[0] == 0x2F)  //Demura
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        txttip.Text += "Demura RAM Enable\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        txttip.Text += "Demura RAM Addr2 Enable\r\n";
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        txttip.Text += "Demura RAM Addr3 Addr Enable\r\n";
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        txttip.Text += "Demura RAM Addr4 Addr Enable\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        txttip.Text += "Demura RAM Disable\r\n";
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0xff)
                                        {
                                            txttip.Text += "Demura RAM write Flash fail ! \r\n";
                                            // MessageBox.Show("载入失败！");
                                        }
                                        else
                                        {
                                            txttip.Text += Rec_tmp[4].ToString("x2") + "Demura RAM Write Flash Complete ! \r\n";
                                            // MessageBox.Show("载入完成！");
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        txttip.Text += Rec_tmp[4].ToString("x2") + "Demura RAM Write Flash2 Complete ! \r\n";
                                        // MessageBox.Show("载入完成！");
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        txttip.Text += Rec_tmp[4].ToString("x2") + "Demura RAM Write Flash3 Complete ! \r\n";
                                        // MessageBox.Show("载入完成！");
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        txttip.Text += Rec_tmp[4].ToString("x2") + "Demura RAM Write Flash4 Complete ! \r\n";
                                        //  MessageBox.Show("载入完成！");
                                    }
                                    break;

                                case 0x04:
                                    {
                                        txttip.Text += "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "," + Rec_tmp[7].ToString("x") + "\r\n";
                                    }
                                    break;
                                case 0x05:
                                    {
                                        txttip.Text += "Flash Erase Success ! \r\n";
                                    }
                                    break;
                                case 0x06:
                                    {
                                        txttip.Text += "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                    }
                                    break;
                                case 0x07:
                                    {
                                        txttip.Text += "Demura function on \r\n";
                                    }
                                    break;
                                case 0x08:
                                    {
                                        txttip.Text += "Demura function off \r\n";
                                    }
                                    break;
                                case 0x09:
                                    {
                                        txttip.Text += "Demura OTP complete \r\n";
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        else
                        {

                        }
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }


        private void VTDR610C_Others_Click(object sender, EventArgs e)
        {

            string str = regSend.Text.ToString();

            string[] arr;
            arr = str.Split(',');
            Setting.TempRecord.cmdLen = arr.Length + 5;
            Send_buf = new byte[Setting.TempRecord.cmdLen];
            Send_buf[2] = Convert.ToByte(arr.Length);
            Button btnTmp = (Button)sender;
            Send_buf[0] = 0x6B;

            switch (btnTmp.Name)
            {
                case "writeReg":
                    {
                        VTDR6100_0XFE_XX = 0x00;
                        Send_buf[1] = 0x01;
                    }
                    break;
                case "readReg":
                    {
                        VTDR6100_0XFE_XX = 0x00;
                        Send_buf[1] = 0x02;
                    }
                    break;

                default:
                    break;
            }
            for (int i = 0; i < arr.Length; i++)
            {
                Send_buf[3 + i] = byte.Parse(Convert.ToUInt32(arr[i], 16).ToString());
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[Setting.TempRecord.cmdLen - 2], out Send_buf[Setting.TempRecord.cmdLen - 1]);
            try
            {
                frmTop.comPort.Write(Send_buf, 0, Setting.TempRecord.cmdLen);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmTop.T1.Enabled = false;
            frmTop.T1.Interval = 2000;
            frmTop.T1.Enabled = true;
        }


        private void btnRun_Click(object sender, EventArgs e)
        {
            frmTop.btnRun_Click(frmTop.btnRun, e);
        }

        private void btnSaveGamma_Click(object sender, EventArgs e)
        {
            string tepGammaR = ""; string tepGammaG = ""; string tepGammaB = "";
            string tepstr = "";
            string vtdrGammatxt = "";
            Int16 gdattmp;
            {
                Button btnTmp = (Button)sender;
                switch (btnTmp.Name)
                {
                    case "btnSaveGamma":
                        {
                            //--------------------保存云英谷格式----------------------------
                            //以文本形式保存
                            if (chkGammaFormatForVTDR.Checked)
                            {
                                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                {
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString());      //变换格式
                                    tepGammaR += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString());
                                    tepGammaG += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString());
                                    tepGammaB += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                }
                                tepstr = "B0," + tepGammaR.Substring(1 - 1, 48 - 1) + "\r\n" + "B1," + tepGammaR.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B2," + tepGammaR.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B3," + tepGammaR.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                vtdrGammatxt += "//GammaR, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B4," + tepGammaG.Substring(1 - 1, 48 - 1) + "\r\n" + "B5," + tepGammaG.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B6," + tepGammaG.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B7," + tepGammaG.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                vtdrGammatxt += "//GammaG, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B8," + tepGammaB.Substring(1 - 1, 48 - 1) + "\r\n" + "B9," + tepGammaB.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "BA," + tepGammaB.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "BB," + tepGammaB.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                vtdrGammatxt += "//GammaB, L-->H : ...\r\n" + tepstr + "\r\n";

                                string str = "";
                                if (rbt_VTDR6110C_Gamma9.Checked) { str = "// ---Gamma 9 九---\r\n //Gamma set 9---\r\n bf,08\r\n"; }
                                else if (rbt_VTDR6110C_Gamma1.Checked) { str = "// ---Gamma 1 一---\r\n //Gamma set 1---\r\n bf,00\r\n"; }
                                else if (rbt_VTDR6110C_Gamma2.Checked) { str = "// ---Gamma 2 二---\r\n //Gamma set 2---\r\n bf,01\r\n"; }
                                else if (rbt_VTDR6110C_Gamma3.Checked) { str = "// ---Gamma 3 三---\r\n //Gamma set 3---\r\n bf,02\r\n"; }
                                else if (rbt_VTDR6110C_Gamma4.Checked) { str = "// ---Gamma 4 四---\r\n //Gamma set 4---\r\n bf,03\r\n"; }
                                else if (rbt_VTDR6110C_Gamma5.Checked) { str = "// ---Gamma 5 五---\r\n //Gamma set 5---\r\n bf,04\r\n"; }
                                else if (rbt_VTDR6110C_Gamma6.Checked) { str = "// ---Gamma 6 六---\r\n //Gamma set 6---\r\n bf,05\r\n"; }
                                else if (rbt_VTDR6110C_Gamma7.Checked) { str = "// ---Gamma 7 七---\r\n //Gamma set 7---\r\n bf,06\r\n"; }
                                else if (rbt_VTDR6110C_Gamma8.Checked) { str = "// ---Gamma 8 八---\r\n //Gamma set 8---\r\n bf,07\r\n"; }
                                //------------------------------------------------------------------------
                                SaveFileDialog txt = new SaveFileDialog();
                                txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                                //string GammaStr = "//-----保存Gamma 寄存器值-----\r\n";
                                if (txt.ShowDialog() == DialogResult.OK)
                                {
                                    if (txt.FileName == " ")
                                    {
                                        return;
                                    }
                                    StreamWriter gsw = null; //文件流
                                    gsw = new StreamWriter(txt.FileName, false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                                    try
                                    {
                                        richTextBox_GC_In.Text += str + vtdrGammatxt;
                                        gsw.Write(str + vtdrGammatxt);
                                        gsw.Dispose(); //释放资源
                                        gsw.Close(); //关闭数据流
                                        MessageBox.Show("保存Gamma 寄存器保存OK！", "系统提示：");
                                    }
                                    catch
                                    {
                                        MessageBox.Show("保存Gamma 寄存器保存时发生错误，请再试！", "系统提示：");
                                    }
                                }
                            }
                            else if (chkvtdrdircshow.Checked)
                            {
                                for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                {
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString());      //变换格式
                                    tepGammaR += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString());
                                    tepGammaG += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                    gdattmp = Convert.ToInt16(this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString());
                                    tepGammaB += string.Format("{0:X2}", (gdattmp >> 8)) + "," + string.Format("{0:X2}", (gdattmp & 0xff)) + ",";
                                }

                                tepstr = "B0," + tepGammaR.Substring(1 - 1, 48 - 1) + "\r\n" + "B1," + tepGammaR.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B2," + tepGammaR.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B3," + tepGammaR.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                vtdrGammatxt += "//GammaR, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B4," + tepGammaG.Substring(1 - 1, 48 - 1) + "\r\n" + "B5," + tepGammaG.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "B6," + tepGammaG.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "B7," + tepGammaG.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                vtdrGammatxt += "//GammaG, L-->H : ...\r\n" + tepstr + "\r\n";

                                tepstr = "B8," + tepGammaB.Substring(1 - 1, 48 - 1) + "\r\n" + "B9," + tepGammaB.Substring(1 + 48 - 1, 48 - 1) + "\r\n" + "BA," + tepGammaB.Substring(1 + 48 + 48 - 1, 48 - 1) + "\r\n" + "BB," + tepGammaB.Substring(1 + 48 + 48 + 48 - 1, 6 - 1) + "\r\n";
                                vtdrGammatxt += "//GammaB, L-->H : ...\r\n" + tepstr + "\r\n";
                                //------------------------------------------------------------------------
                                string str = "";
                                if (rbt_VTDR6110C_Gamma9.Checked) { str = "// ---Gamma 9 九---\r\n //Gamma set 9---\r\n bf,08\r\n"; }
                                else if (rbt_VTDR6110C_Gamma1.Checked) { str = "// ---Gamma 1 一---\r\n //Gamma set 1---\r\n bf,00\r\n"; }
                                else if (rbt_VTDR6110C_Gamma2.Checked) { str = "// ---Gamma 2 二---\r\n //Gamma set 2---\r\n bf,01\r\n"; }
                                else if (rbt_VTDR6110C_Gamma3.Checked) { str = "// ---Gamma 3 三---\r\n //Gamma set 3---\r\n bf,02\r\n"; }
                                else if (rbt_VTDR6110C_Gamma4.Checked) { str = "// ---Gamma 4 四---\r\n //Gamma set 4---\r\n bf,03\r\n"; }
                                else if (rbt_VTDR6110C_Gamma5.Checked) { str = "// ---Gamma 5 五---\r\n //Gamma set 5---\r\n bf,04\r\n"; }
                                else if (rbt_VTDR6110C_Gamma6.Checked) { str = "// ---Gamma 6 六---\r\n //Gamma set 6---\r\n bf,05\r\n"; }
                                else if (rbt_VTDR6110C_Gamma7.Checked) { str = "// ---Gamma 7 七---\r\n //Gamma set 7---\r\n bf,06\r\n"; }
                                else if (rbt_VTDR6110C_Gamma8.Checked) { str = "// ---Gamma 8 八---\r\n //Gamma set 8---\r\n bf,07\r\n"; }
                                Points_LV.Text += (str + vtdrGammatxt);
                                richTextBox_GC_In.Text += (str + vtdrGammatxt);
                            }
                            else
                            {//保存
                                SaveFileDialog sf = new SaveFileDialog(); //对话框
                                sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                                string str = "灰阶序号,灰阶亮度,reg_R,reg_G,reg_B\r\n";
                                if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                                {
                                    if (sf.FileName == "") return; //如果没有输入文件名
                                    try
                                    {
                                        StreamWriter sw = null; //文件流
                                        sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                        for (int i = 0; i < (this.GammaReg_dataGridView.Rows.Count - 1); i++)
                                        {
                                            str = str + this.GammaReg_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[3].Value.ToString() + ","
                                                + this.GammaReg_dataGridView.Rows[i].Cells[4].Value.ToString() + "\r\n";
                                        }
                                        sw.WriteLine(str);
                                        sw.Dispose(); //释放资源
                                        sw.Close(); //关闭数据流
                                        MessageBox.Show("数据保存OK！", "系统提示：");

                                    }
                                    catch
                                    {
                                        MessageBox.Show("数据保存时发生错误，请再试！", "系统提示：");
                                    }
                                }

                            }
                        }
                        break;
                    case "btnLoadGamma":
                        {
                            this.GammaReg_dataGridView.Rows.Clear();
                            OpenFileDialog sf = new OpenFileDialog();
                            int Line_Count = 0;
                            string strtmp = "";
                            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                            sf.Title = "数据导入";
                            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                            {
                                FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                                string strLine = streamReader.ReadLine();
                                while (!string.IsNullOrEmpty(strLine))
                                {
                                    strtmp = strtmp + strLine + ",";
                                    strLine = streamReader.ReadLine(); //循环读取下一行
                                    Line_Count++;
                                }
                                fs.Close(); //关闭流
                                streamReader.Close();
                                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                                for (int j = 1; j <= Line_Count - 1; j++)
                                {
                                    DataGridViewRow Row1 = new DataGridViewRow();
                                    GammaReg_dataGridView.Rows.Add(Row1);
                                }
                                try
                                {
                                    for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                                    {
                                        GammaReg_dataGridView.Rows[i].Cells[0].Value = arrtmp[5 * (i + 1) + 0];
                                        GammaReg_dataGridView.Rows[i].Cells[1].Value = arrtmp[5 * (i + 1) + 1];
                                        GammaReg_dataGridView.Rows[i].Cells[2].Value = arrtmp[5 * (i + 1) + 2];
                                        GammaReg_dataGridView.Rows[i].Cells[3].Value = arrtmp[5 * (i + 1) + 3];
                                        GammaReg_dataGridView.Rows[i].Cells[4].Value = arrtmp[5 * (i + 1) + 4];
                                        //------------------------------------
                                        GammaValue[i, 0] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 2]);
                                        GammaValue[i, 1] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 3]);
                                        GammaValue[i, 2] = Convert.ToUInt32(arrtmp[(5 * (i + 1)) + 4]);
                                    }
                                    MessageBox.Show("数据导入OK！", "提示：");
                                }
                                catch
                                {
                                    MessageBox.Show("数据导入时发生错误！", "提示：");
                                }
                            }
                        }
                        break;
                    case "btn_CAS200_Clean":
                        {
                            DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                            if (dr == DialogResult.OK)
                            {
                                this.GammaReg_dataGridView.Rows.Clear();
                            }
                        }
                        break;

                    default: break;
                }
            }
        }

        private void btnWriteGamma_Click(object sender, EventArgs e)
        {
            string tepcmd;
            if (txtCmd3.Text != "")
            {
                tepcmd = txtCmd3.Text;
                // GammaValueFormat_Click(GammaValueFormat, null);
                //txtCmd3.Text = txtGammaCodeSample.Text + txtGammadisplay.Text + "\r\nC4,00";
                Send_Cmd_Click(btnSend3, null);
                txtCmd3.Text = tepcmd;
            }
            else
            {
                // GammaValueFormat_Click(GammaValueFormat, null);
                //txtCmd3.Text = txtGammaCodeSample.Text + txtGammadisplay.Text + "\r\nC4,00";
                Send_Cmd_Click(btnSend3, null);
            }

        }

        private double D_x, D_y;

        //private int index = 0;
        private double lum_off, x_off, y_off;
        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;

        private double G_y = 0.71;
        private double B_x = 0.14;
        private double B_y = 0.06;
        private uint[,] GammaValue = new uint[26, 3];//保存当前Gammavalue
        static byte[] GetCRCArray(byte[] arr, Byte speedflag)
        {
            CRC16 CRCData = new CRC16();

            int arrNumbuer;                                                                                                                                                                                                                                                                                                                                             //51
            arrNumbuer = arr.Length;
            arrNumbuer += 6;
            byte[] Send_Datax = new byte[arrNumbuer];
            Send_Datax[0] = 0x9f;
            Send_Datax[1] = (byte)(speedflag << 4);
            Send_Datax[2] = Convert.ToByte(arr.Length + 1);  //为兼容下位机命令，此位置填待传输有效数据长度+1 

            Send_Datax[arr.Length + 3] = 0xFF;  //添加结束标志位
            arr.CopyTo(Send_Datax, 3);//合并数组头 4个Byte 标志位
            //计算校验码                                                 
            CRCData.CalculateCrc16(Send_Datax, out Send_Datax[arrNumbuer - 2], out Send_Datax[arrNumbuer - 1]);
            return Send_Datax;
        }
        byte[] GammaValueTeparr = new byte[54 * 3]; //存放格式化后的GammaValue，以便于直接下发至DDIC
        byte[] GammaValueReceived = new byte[50 * 3]; //存放从下位机接收过来的GammaValue
        private void GammaValueFormat_Click(object sender, EventArgs e)
        {
            //txtGammadisplay.Text = "B0"; //Convert.ToString(要转的数,16);
            for (int i = 0; i < 8; i++)
            {
                // txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 0] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 0] & 0xff, 16);
                GammaValueTeparr[0] = 0xb0;
                GammaValueTeparr[2 * i + 1] = (byte)(GammaValue[i, 0] >> 8);
                GammaValueTeparr[2 * i + 2] = (byte)(GammaValue[i, 0] & 0xff);
            }
            //txtGammadisplay.Text += "\r\n" + "B1"; //Convert.ToString(要转的数,16);
            for (int i = 8; i < 16; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 0] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 0] & 0xff, 16);
                GammaValueTeparr[0 + 17] = 0xb1;
                GammaValueTeparr[2 * i + 2] = (byte)(GammaValue[i, 0] >> 8);
                GammaValueTeparr[2 * i + 3] = (byte)(GammaValue[i, 0] & 0xff);
            }
            //txtGammadisplay.Text += "\r\n" + "B2"; //Convert.ToString(要转的数,16);
            for (int i = 16; i < 24; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 0] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 0] & 0xff, 16);
                GammaValueTeparr[0 + 34] = 0xb2;
                GammaValueTeparr[2 * i + 3] = (byte)(GammaValue[i, 0] >> 8);
                GammaValueTeparr[2 * i + 4] = (byte)(GammaValue[i, 0] & 0xff);
            }
            // txtGammadisplay.Text += "\r\n" + "B3"; //Convert.ToString(要转的数,16);
            for (int i = 24; i < 25; i++)
            {
                // txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 0] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 0] & 0xff, 16);
                GammaValueTeparr[0 + 51] = 0xb3;
                GammaValueTeparr[2 * i + 4] = (byte)(GammaValue[i, 0] >> 8);
                GammaValueTeparr[2 * i + 5] = (byte)(GammaValue[i, 0] & 0xff);
            }
            //-------------
            // txtGammadisplay.Text += "\r\n" + "B4"; //Convert.ToString(要转的数,16);
            for (int i = 0; i < 8; i++)
            {
                // txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 1] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 1] & 0xff, 16);
                GammaValueTeparr[54 + 0] = 0xb4;
                GammaValueTeparr[54 + 2 * i + 1] = (byte)(GammaValue[i, 1] >> 8);
                GammaValueTeparr[54 + 2 * i + 2] = (byte)(GammaValue[i, 1] & 0xff);
            }
            //txtGammadisplay.Text += "\r\n" + "B5"; //Convert.ToString(要转的数,16);
            for (int i = 8; i < 16; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 1] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 1] & 0xff, 16);
                GammaValueTeparr[54 + 0 + 17] = 0xb5;
                GammaValueTeparr[54 + 2 * i + 2] = (byte)(GammaValue[i, 1] >> 8);
                GammaValueTeparr[54 + 2 * i + 3] = (byte)(GammaValue[i, 1] & 0xff);
            }
            //txtGammadisplay.Text += "\r\n" + "B6"; //Convert.ToString(要转的数,16);
            for (int i = 16; i < 24; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 1] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 1] & 0xff, 16);
                GammaValueTeparr[54 + 0 + 34] = 0xb6;
                GammaValueTeparr[54 + 2 * i + 3] = (byte)(GammaValue[i, 1] >> 8);
                GammaValueTeparr[54 + 2 * i + 4] = (byte)(GammaValue[i, 1] & 0xff);
            }
            //txtGammadisplay.Text += "\r\n" + "B7"; //Convert.ToString(要转的数,16);
            for (int i = 24; i < 25; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 1] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 1] & 0xff, 16);
                GammaValueTeparr[54 + 0 + 51] = 0xb7;
                GammaValueTeparr[54 + 2 * i + 4] = (byte)(GammaValue[i, 1] >> 8);
                GammaValueTeparr[54 + 2 * i + 5] = (byte)(GammaValue[i, 1] & 0xff);
            }

            //txtGammadisplay.Text += "\r\n" + "B8"; //Convert.ToString(要转的数,16);
            for (int i = 0; i < 8; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 2] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 2] & 0xff, 16);
                GammaValueTeparr[108 + 0 + 0] = 0xb8;
                GammaValueTeparr[108 + 2 * i + 1] = (byte)(GammaValue[i, 2] >> 8);
                GammaValueTeparr[108 + 2 * i + 2] = (byte)(GammaValue[i, 2] & 0xff);
            }
            //txtGammadisplay.Text += "\r\n" + "B9"; //Convert.ToString(要转的数,16);
            for (int i = 8; i < 16; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 2] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 2] & 0xff, 16);
                GammaValueTeparr[108 + 0 + 17] = 0xb9;
                GammaValueTeparr[108 + 2 * i + 2] = (byte)(GammaValue[i, 2] >> 8);
                GammaValueTeparr[108 + 2 * i + 3] = (byte)(GammaValue[i, 2] & 0xff);
            }
            //txtGammadisplay.Text += "\r\n" + "BA"; //Convert.ToString(要转的数,16);
            for (int i = 16; i < 24; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 2] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 2] & 0xff, 16);
                GammaValueTeparr[108 + 0 + 34] = 0xba;
                GammaValueTeparr[108 + 2 * i + 3] = (byte)(GammaValue[i, 2] >> 8);
                GammaValueTeparr[108 + 2 * i + 4] = (byte)(GammaValue[i, 2] & 0xff);
            }
            //txtGammadisplay.Text += "\r\n" + "BB"; //Convert.ToString(要转的数,16);
            for (int i = 24; i < 25; i++)
            {
                //txtGammadisplay.Text += "," + Convert.ToString(GammaValue[i, 2] >> 8, 16) + "," + Convert.ToString(GammaValue[i, 2] & 0xff, 16);
                GammaValueTeparr[108 + 0 + 51] = 0xbb;
                GammaValueTeparr[108 + 2 * i + 4] = (byte)(GammaValue[i, 2] >> 8);
                GammaValueTeparr[108 + 2 * i + 5] = (byte)(GammaValue[i, 2] & 0xff);
            }
        }

        private int[] Point_VTDR6110C = new int[25] {//---Gamma绑点数据，这里的数据仅供参考---
            0,1,3,5,7,11,15,19,23,27,31,35,39,47,55,63,71,79,95,111,127,159,191,223,255};

        //
        private void btnCalGammaValue_Click(object sender, EventArgs e)
        {
            D_lum = new double[25];
            // DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
            // if (dr == DialogResult.OK)
            {
                this.GammaReg_dataGridView.Rows.Clear();
            }

            for (byte i = 0; i < 25; i++)
            {
                D_lum[i] = (Math.Pow((Point_VTDR6110C[i] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text));
                DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
            }

        }
        private void GammaValueSet(byte rh, byte rl, byte gh, byte gl, byte bh, byte bl, byte[] Gammaarr)
        {   //通过高速方式向第一条Gamma写入一组Gamma值
            byte[] TepArr;
            try
            {
                TepArr = new byte[] { 3, 0xf0, 0xaa, 0x13,
                    4, 0XFF, 0XFF, 0XFF, 25,
                    2, 0xbf, 0x08,   //第9条Gamma
                    4, 0XFF, 0XFF, 0XFF, 25,
                    2, 0xc4, 0x01,
                    4, 0XFF, 0XFF, 0XFF, 25 };  //byte[] TepArr;

                // 设定对应Gamma序号
                if (rbt_VTDR6110C_Gamma1.Checked)
                {
                    Gammnum = 0x00;
                }
                else if (rbt_VTDR6110C_Gamma2.Checked)
                {
                    Gammnum = 0x01;
                }
                else if (rbt_VTDR6110C_Gamma3.Checked)
                {
                    Gammnum = 0x02;
                }
                else if (rbt_VTDR6110C_Gamma4.Checked)
                {
                    Gammnum = 0x03;
                }
                else if (rbt_VTDR6110C_Gamma5.Checked)
                {
                    Gammnum = 0x04;
                }
                else if (rbt_VTDR6110C_Gamma6.Checked)
                {
                    Gammnum = 0x05;
                }
                else if (rbt_VTDR6110C_Gamma7.Checked)
                {
                    Gammnum = 0x06;
                }
                else if (rbt_VTDR6110C_Gamma8.Checked)
                {
                    Gammnum = 0x07;
                }
                else if (rbt_VTDR6110C_Gamma9.Checked)
                {
                    Gammnum = 0x08;
                }

                TepArr[11] = Gammnum; //设定对应Gamma序号

                frmTop.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾  
                //-----------------  
                TepArr = new byte[] {
                17,0xB0, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                17,0xB1, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                17,0xB2, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                3, 0xB3, 00, 00,
                17,0xB4, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                17,0xB5, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                17,0xB6, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                3, 0xB7, 03, 00,
                17,0xB8, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                17,0xB9, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                17,0xBa, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 ,
                3, 0xBb, 00, 00,
                2,0xc4, 0x00
                };

                Array.Copy(Gammaarr, 0, TepArr, 1, 17);
                Array.Copy(Gammaarr, 17, TepArr, 1 + 18 * 1, 17);
                Array.Copy(Gammaarr, 34, TepArr, 1 + 18 * 2, 17);
                Array.Copy(Gammaarr, 51, TepArr, 1 + 18 * 3, 3);

                Array.Copy(Gammaarr, 54, TepArr, 1 + 18 * 3 + 4, 17);
                Array.Copy(Gammaarr, 71, TepArr, 1 + 18 * 4 + 4, 17);
                Array.Copy(Gammaarr, 88, TepArr, 1 + 18 * 5 + 4, 17);
                Array.Copy(Gammaarr, 105, TepArr, 1 + 18 * 6 + 4, 3);

                Array.Copy(Gammaarr, 108, TepArr, 1 + 18 * 6 + 8, 17);
                Array.Copy(Gammaarr, 125, TepArr, 1 + 18 * 7 + 8, 17);
                Array.Copy(Gammaarr, 142, TepArr, 1 + 18 * 8 + 8, 17);
                Array.Copy(Gammaarr, 159, TepArr, 1 + 18 * 9 + 8, 3);
                //public static void Copy(
                //Array sourceArray,
                //int sourceIndex,
                //Array destinationArray,
                //int destinationIndex,
                //int length); 
                frmTop.comPort.Write(GetCRCArray(TepArr, 4), 0, TepArr.Length + 6);//循环部分，每段数据结尾
                //Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
        }
        private void btnAutoGammas_VTDR6100_Click(object sender, EventArgs e)
        {
            int flag = 1;
            STOPAutoGammFlag = false;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[25, 3];
            D_lum = new double[25];
            index = 2;  //末尾绑点不调算了
            //AutoGamma 时需要先设定51值
            //if (rbt_VTDR6110C_Gamma5.Checked)        { regSend.Text = "51,01,c6"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } }
            //else if (rbt_VTDR6110C_Gamma6.Checked) { regSend.Text = "51,03,d1"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } }
            //else if (rbt_VTDR6110C_Gamma7.Checked) { regSend.Text = "51,05,dc"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } }
            //else if (rbt_VTDR6110C_Gamma8.Checked) { regSend.Text = "51,07,ff";  if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } }
            //else if (rbt_VTDR6110C_Gamma9.Checked) { regSend.Text = "51,0f,ff";  if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } }
            labTargetlum.Text = "Tar:" + txtVT_lum.Text;
            rstep = (byte)(int.Parse(numStep.Value.ToString()));
            gstep = (byte)(int.Parse(numStep.Value.ToString()));
            bstep = (byte)(int.Parse(numStep.Value.ToString()));

            TimTest.Start();
            TimeNow = DateTime.Now; //开始计时

            for (int i = 0; i < 25; i++)
            {
                D_lum[i] = (Math.Pow((Point_VTDR6110C[i] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text));
            }

            D_x = Convert.ToDouble(txtVT_x.Text);
            D_y = Convert.ToDouble(txtVT_y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 25; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        //lum_off = Convert.ToDouble(txtVT_loff.Text);
                        x_off = Convert.ToDouble(txtVT_xoff.Text);
                        y_off = Convert.ToDouble(txtVT_yoff.Text);
                        OLED_Show_Click(btnwhite, e);
                        ReceivOKFlag = false;
                        T17.Enabled = true;
                    }
                    break;
                case 2:  //寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }
        private double auto_lum;//
        private double auto_x;
        private double auto_y;
        private uint step_l = 1;
        private void Msr_CA310()
        {
            //CASDK_LUM = CaSDKObj.objProbe.Lv;
            //CASDK_x = CaSDKObj.objProbe.sx;
            //CASDK_y = CaSDKObj.objProbe.sy;
            frmTop.Msr_CASDK();
            auto_lum = frmTop.CASDK_LUM;
            auto_x = frmTop.CASDK_x;
            auto_y = frmTop.CASDK_y;
        }
        bool ReceivOKFlag = false;
        bool dirupR = true; bool dirupG = true; bool dirupB = true; // Gamma 调整方向
        bool lastdirupR = true; bool lastdirupG = true; bool lastdirupB = true; // Gamma 调整方向
        byte rstep = 16; byte gstep = 16; byte bstep = 16;
        private void T17_Tick(object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            T17.Enabled = false;
            T17.Interval = 200;
            //if (!ReceivOKFlag)
            //{
            //    numPatternR.Value = Point_VTDR6110C[26 - index];
            //    numPatternG.Value = Point_VTDR6110C[26 - index];
            //    numPatternB.Value = Point_VTDR6110C[26 - index];
            //    OLED_Show_Click(btnPatternW, e);//写入Pattern参数
            //}
            Msr_CA310();

            txtlvshow.Text = "Lv: " + auto_lum + "\r\n" + "x: " + auto_x + "\r\n" + "y: " + auto_y + "\r\n";
            if (index > 2)
                labTargetlum.Text = "Tar:" + D_lum[26 - index];

            Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
            Lb_t = 1.0;
            Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
            lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
            lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
            lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

            lum_off = (D_lum[26 - index] * Convert.ToDouble(numVT_loff.Value) / 1000);
            x_off = Convert.ToDouble(txtVT_xoff.Text);
            y_off = Convert.ToDouble(txtVT_yoff.Text);

            if ((auto_lum < (D_lum[26 - index] + lum_off)) && (auto_lum > (D_lum[26 - index] - lum_off)))//
                flag1 = 0;
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//
                flag2 = 0;
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0)//
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T17.Enabled = false;
                index++;
                rstep = (byte)(int.Parse(numStep.Value.ToString()));
                gstep = (byte)(int.Parse(numStep.Value.ToString()));
                bstep = (byte)(int.Parse(numStep.Value.ToString()));
                if (index == 25)
                {
                    TimTest.Stop();//停止计时
                    txttip.Text = txttip.Text + "VTDR6100 gamma调试完毕!\r\n";
                    txttip.Focus();//获取焦点
                    txttip.Select(txttip.TextLength, 0);//光标定位到文本最后
                    txttip.ScrollToCaret();//滚动到光标处
                    //FirstAdjGammaFlag = true;
                    return;
                }
                if (index < 26)
                {
                    txttip.Text += index - 2 + "" + "完成...\r\n";
                    txttip.Select(txttip.Text.Length, 0);
                    txttip.ScrollToCaret();

                    for (int i = 0; i < 25; i++)
                    {
                        //Convert.ToString(要转的数, 16);//string s = string.Format("0x{0:X2}", i);    // s = 0x0F;
                        //Points_LV.Text += (i + 1) + ",R/G/B, V" + Point_VTDR6110C[24 - i] + ": " + string.Format("0x{0:X2}", GammaValue[24 - i, 0]) + "--";
                        //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 1]) + "--";
                        //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 2]) + "\r\n";

                        //this.GammaReg_dataGridView.Rows.Clear();
                        //DataGirdShow((byte)i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                        //Points_LV.Text += (i + 1) + ",R/G/B, V" + Point_VTDR6110C[24 - i] + ": " + string.Format("0x{0:X2}", GammaValue[24 - i, 0]) + "--";
                        //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 1]) + "--";
                        //Points_LV.Text += string.Format("0x{0:X2}", GammaValue[24 - i, 2]) + "\r\n";
                    }

                    Thread.Sleep(10); //1秒=1000毫秒，
                    //T17.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
                numPatternR.Value = Point_VTDR6110C[26 - index];
                numPatternG.Value = Point_VTDR6110C[26 - index];
                numPatternB.Value = Point_VTDR6110C[26 - index];
                OLED_Show_Click(btnPatternW, e);//写入Pattern参数
                ReceivOKFlag = false;
                Thread.Sleep(10);
                if (!STOPAutoGammFlag)
                    T17.Enabled = true;
            }
            else
            {
                //Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                //Lb_t = 1.0;
                //Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                //lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                //lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                //lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                //try
                {
                    step_l = (byte)(int.Parse(numStep.Value.ToString()));

                    switch (index)
                    {
                        case 0:
                        case 1:
                            {
                                GammaValue[0, 1] = 0;  //0绑点不调，直接送零
                                GammaValue[0, 2] = 0;
                                GammaValue[0, 0] = 0;
                                GammaValue[1, 1] = 0;  //1绑点不调，直接送零
                                GammaValue[1, 2] = 0;
                                GammaValue[1, 0] = 0;

                                index++;
                            }
                            break;

                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12:
                        case 13:
                        case 14:
                        case 15:
                        case 16:
                        case 17:
                        case 18:
                        case 19:
                        case 20:
                        case 21:
                        case 22:
                        case 23:
                        case 24:
                            {
                                if ((flag3 == 1) | (flag2 == 1) | (flag1 == 1))
                                {

                                    {
                                        if (lumr > D_lums[26 - index, 0])
                                        {
                                            if ((!lastdirupR) && (rstep >= 2)) rstep >>= 1;
                                            GammaValue[26 - index, 0] = GammaValue[26 - index, 0] - rstep;// (byte)(int.Parse(numStep.Value.ToString()));
                                            lastdirupR = true;
                                            // rstep = 16;
                                        }

                                        else
                                        {
                                            if ((lastdirupR) && (rstep >= 2)) rstep >>= 1;
                                            GammaValue[26 - index, 0] = GammaValue[26 - index, 0] + rstep;
                                            lastdirupR = false;
                                            //rstep = 32;
                                        }
                                        /*if (VTDR6100_R127.Value <= VTDR6100_R111.Value)
                                            VTDR6100_R127.Value = VTDR6100_R111.Value + step_h;*/

                                    }

                                    //}
                                    //if (flag1 == 1)
                                    {

                                        {
                                            if (lumg > D_lums[26 - index, 1])
                                            {
                                                if ((!lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                                GammaValue[26 - index, 1] = GammaValue[26 - index, 1] - gstep;
                                                lastdirupG = true;
                                            }
                                            else
                                            {
                                                if ((lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                                GammaValue[26 - index, 1] = GammaValue[26 - index, 1] + gstep;
                                                lastdirupG = false;
                                            }
                                            /*if (VTDR6100_G127.Value <= VTDR6100_G111.Value)
                                                VTDR6100_G127.Value = VTDR6100_G111.Value + step_h;*/
                                        }
                                        //adjust_Gnum++;
                                    }
                                    //if (flag2 == 1)
                                    //{

                                    {
                                        if (lumb > D_lums[26 - index, 2])
                                        {
                                            if ((!lastdirupB) && (bstep >= 2)) bstep >>= 1;
                                            GammaValue[26 - index, 2] = GammaValue[26 - index, 2] - bstep;
                                            lastdirupB = true;
                                        }
                                        else
                                        {
                                            if ((lastdirupG) && (gstep >= 2)) gstep >>= 1;
                                            GammaValue[26 - index, 2] = GammaValue[26 - index, 2] + bstep;
                                            lastdirupB = false;
                                        }
                                        /*if (VTDR6100_B127.Value <= VTDR6100_B111.Value)
                                            VTDR6100_B127.Value = VTDR6100_B111.Value + step_h;*/
                                        txttip.Text += "rstep:" + rstep + "gstetp:" + gstep + "bstep:" + bstep + "\r\n";
                                        txttip.Select(txttip.Text.Length, 0);
                                        txttip.ScrollToCaret();
                                    }

                                }

                                for (int i = 24; i > 0; i--)
                                {
                                    if (GammaValue[i, 0] < GammaValue[i - 1, 0])
                                    {
                                        GammaValue[i - 1, 0] = GammaValue[i, 0] - 0;
                                        //GammaValueFormat_Click(null, null);
                                    }
                                    if (GammaValue[i, 1] < GammaValue[i - 1, 1])
                                    {
                                        GammaValue[i - 1, 1] = GammaValue[i, 1] - 0;
                                        //GammaValueFormat_Click(null, null);
                                    }
                                    if (GammaValue[i, 2] < GammaValue[i - 1, 2])
                                    {
                                        GammaValue[i - 1, 2] = GammaValue[i, 2] - 0;
                                        //GammaValueFormat_Click(null, null);
                                    }

                                }
                                if (chkAutoSet51.Checked)
                                {
                                    this.GammaReg_dataGridView.Rows.Clear();
                                    for (byte i = 0; i < 25; i++)
                                    {
                                        DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                                    }
                                }

                                GammaValueFormat_Click(null, null);

                                byte rh = (Byte)(GammaValue[26 - index, 0] >> 8);
                                byte rl = (Byte)(GammaValue[26 - index, 0] & 0xff);
                                byte gh = (Byte)(GammaValue[26 - index, 1] >> 8);
                                byte gl = (Byte)(GammaValue[26 - index, 1] & 0xff);
                                byte bh = (Byte)(GammaValue[26 - index, 2] >> 8);
                                byte bl = (Byte)(GammaValue[26 - index, 2] & 0xff);
                                txtGammaValue.Text = GammaValue[26 - index, 0] + "\r\n" + GammaValue[26 - index, 1] + "\r\n" + GammaValue[26 - index, 2] + "\r\n";
                                GammaValueSet(rh, rl, gh, gl, bh, bl, GammaValueTeparr);
                                Thread.Sleep(100);
                                Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                                //SendGamma2VTDR();
                                //Thread.Sleep(100);
                            }
                            break;
                        case 25:
                            {
                                //for (int k = 0; k < 13; k++)
                                //{
                                //    numPatternR.Value = Parttern_VTDR6100[k];
                                //    numPatternG.Value = Parttern_VTDR6100[k];
                                //    numPatternB.Value = Parttern_VTDR6100[k];
                                //    OLED_Show_Click(btnPatternW, e);//写入Pattern参数
                                //    Thread.Sleep(1000); //1秒=1000毫秒，
                                //    
                                //    
                                //}
                                T17.Enabled = false;
                            }
                            break;

                        default:
                            T17.Enabled = false;
                            break;
                    }
                    System.Threading.Thread.Sleep(10);
                }
                //catch
                //{
                //    T5.Enabled = false;
                //    MessageBox.Show("数值超限！");
                //    return;
                // }
                if (!STOPAutoGammFlag)
                    T17.Enabled = true;
            }

        }
        bool STOPAutoGammFlag = false;
        private void btnVTStop_Click(object sender, EventArgs e)
        {
            STOPAutoGammFlag = true; ;
            T7.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            TimTest.Stop();//停止计时
            btnCalculateVTDR6100.PerformClick();
        }


        #region  //Gamma调整计时
        System.DateTime TimeNow = new DateTime();
        TimeSpan TimeCount = new TimeSpan();
        private void TimTest_Tick(object sender, EventArgs e)
        {
            TimeCount = DateTime.Now - TimeNow;
            txtTimeShow.Text = string.Format("{0:00}:{1:00}:{2:00}", TimeCount.Hours, TimeCount.Minutes, TimeCount.Seconds);
        }
        #endregion

        private byte[] arrbyte_tmp = new byte[250];
        public void DataGirdShow(byte index, double RRR, double GGG, double BBB)
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();

            Row_Accum = this.GammaReg_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.GammaReg_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.GammaReg_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.GammaReg_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.GammaReg_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.GammaReg_dataGridView.Rows.Add(Row1);
            }
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[0].Value = index + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[1].Value = (D_lum[index]).ToString("#0.0000");
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[2].Value = RRR;
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[3].Value = GGG;
            this.GammaReg_dataGridView.Rows[Row_Accum].Cells[4].Value = BBB;

            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.GammaReg_dataGridView.Rows.Count - 1;
                this.GammaReg_dataGridView.Rows[Current_Index].Selected = false;
                this.GammaReg_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            GammaReg_dataGridView.FirstDisplayedScrollingRowIndex = GammaReg_dataGridView.RowCount - 1;
        }
        private void chkRegGrayshow_CheckedChanged(object sender, EventArgs e)
        {   //切换显示Gamma寄存器值或目标灰阶亮度显示
            //arrbyte_tmp = new byte[100];   // string.Format("0x{0:X2}",    ) + "\r\n";
            string txt_tmp = "";
            // if (chkAutoSet51.Checked)
            {
                for (byte i = 0; i < 25; i++)
                {
                    DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                }

                //txtGammaPointsTargetValue.Text = "";
                //GammaValue[1,0]
            }

        }

        private void GammaReg_dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        #region //通用命令处理
        private void Send_Cmd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            switch (btn.Name)
            {
                case "btnSend1":
                    {
                        frmTop.txtProcessDrivercode(txtCmd1.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend2":
                    {
                        frmTop.txtProcessDrivercode(txtCmd2.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;
                case "btnSend3":
                    {
                        frmTop.txtProcessDrivercode(txtCmd3.Text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                    }
                    break;

            }
            Setting.TempRecord.cmdLen = 6;
            if (rBt_LP_DCS_Long_Write.Checked | rBt_LP_DCS_Short_Write.Checked | rBt_LP_Gerneric_Long_Write.Checked | rBt_LP_Gerneric_Short_Write.Checked)
            {
                frmTop.SendCmd(0);
            }
            else
            {
                frmTop.SendCmd(4);
            }
            txttip.Text += "命令已下发...\r\n";
            txttip.Select(txttip.Text.Length, 0);
            txttip.ScrollToCaret();
        }
        private void SaveCode_Click(object sender, EventArgs e)
        {
            SaveFileDialog txt = new SaveFileDialog();
            txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            string str = "//-----保存Driver IC Code文件------\r\n";
            if (txt.ShowDialog() == DialogResult.OK)
            {
                if (txt.FileName == " ")
                {
                    return;
                }
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(txt.FileName, false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                try
                {
                    if (ChkCMD1.Checked)
                    {
                        str += "//---CMD1---\r\n" + txtCmd1.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD2.Checked)
                    {
                        str += "//---CMD2---\r\n" + txtCmd2.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    if (ChkCMD3.Checked)
                    {
                        str += "//---CMD3---\r\n" + txtCmd3.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n\r\n";
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Driver IC Code保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Driver IC Code保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        private void OpenTxtCode_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            of.Title = "初始化 Code";

            if (of.ShowDialog() == DialogResult.OK)
            {
                StreamReader streamReader = new StreamReader(of.FileName, Encoding.Default);
                switch (btn.Name)
                {
                    case "OpenTxtCode":
                        {
                            txtCmd1.Clear();
                            txtCmd1.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode2":
                        {
                            txtCmd2.Clear();
                            txtCmd2.Text = streamReader.ReadToEnd();
                        }
                        break;
                    case "OpenTxtCode3":
                        {
                            txtCmd3.Clear();
                            txtCmd3.Text = streamReader.ReadToEnd();
                        }
                        break;

                    default: break;
                }
                streamReader.Close();
            }
        }
        #endregion
        string path = "";
        #region //------自定义功能按键---------------
        string speedflag1, speedflag2, speedflag3, speedflag4, speedflag5, speedflag6, speedflag7, speedflag8;

        private void btnFunLoad_Click(object sender, EventArgs e)
        {
            {
                StreamReader streamReader = new StreamReader("./update/Fun.cfg", Encoding.Default);
                FunDefaultfilePath = streamReader.ReadLine();
                streamReader.Close();
            }
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            //首次defaultfilePath为空，按FolderBrowserDialog默认设置（即桌面）选择            
            if (FunDefaultfilePath != "")
            {
                //设置此次默认目录为上一次选中目录
                fldb.SelectedPath = FunDefaultfilePath;
            }

            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            FunDefaultfilePath = fldb.SelectedPath;
            path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            //DirectoryInfo di = new DirectoryInfo(path);
            //this.listBox3_BMPFile.Items.Clear();
            //this.CreateListItem(di);//递归加载文件夹里的所有图片
            //txttip.Text = path;            
            StreamWriter sw = null; //文件流
            sw = new StreamWriter("./update/Fun.cfg", false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
            sw.Write(FunDefaultfilePath);
            sw.Dispose(); //释放资源
            sw.Close(); //关闭数据流
            // -------------------------------------------------------------------------
            try
            {
                //StreamReader streamReader = new StreamReader(path+"\\Fun1.txt", Encoding.Default);
                //FunDefaultfilePath = streamReader.ReadLine();
                //streamReader.Close();
                ReadFuncfg(path + "\\Fun1.txt", out String Ftitle1, out speedflag1, out funcmd1);
                btnFun1.Text = Ftitle1;

                ReadFuncfg(path + "\\Fun2.txt", out String Ftitle2, out speedflag2, out funcmd2);
                btnFun2.Text = Ftitle2;

                ReadFuncfg(path + "\\Fun3.txt", out String Ftitle3, out speedflag3, out funcmd3);
                btnFun3.Text = Ftitle3;

                ReadFuncfg(path + "\\Fun4.txt", out String Ftitle4, out speedflag4, out funcmd4);
                btnFun4.Text = Ftitle4;

                ReadFuncfg(path + "\\Fun5.txt", out String Ftitle5, out speedflag5, out funcmd5);
                btnFun5.Text = Ftitle5;

                ReadFuncfg(path + "\\Fun6.txt", out String Ftitle6, out speedflag6, out funcmd6);
                btnFun6.Text = Ftitle6;

                ReadFuncfg(path + "\\Fun7.txt", out String Ftitle7, out speedflag7, out funcmd7);
                btnFun7.Text = Ftitle7;

                ReadFuncfg(path + "\\Fun8.txt", out String Ftitle8, out speedflag8, out funcmd8);
                btnFun8.Text = Ftitle8;
                btnStore.Enabled = true;
            }
            catch
            {
                MessageBox.Show("向东提示配置文件缺失，请检查", "错误");
            }

        }

        private void CustomSendCmd_Click(object sender, EventArgs e)   //自定义功能按键
        {
            Button btn = (Button)sender; string spedflag = "";
            switch (btn.Name)
            {
                case "btnFun1":
                    {
                        frmTop.txtProcessDrivercode(funcmd1, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        spedflag = speedflag1;
                    }
                    break;
                case "btnFun2":
                    {
                        frmTop.txtProcessDrivercode(funcmd2, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        spedflag = speedflag2;
                    }
                    break;
                case "btnFun3":
                    {
                        frmTop.txtProcessDrivercode(funcmd3, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        spedflag = speedflag3;
                    }
                    break;
                case "btnFun4":
                    {
                        frmTop.txtProcessDrivercode(funcmd4, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        spedflag = speedflag4;
                    }
                    break;
                case "btnFun5":
                    {
                        frmTop.txtProcessDrivercode(funcmd5, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        spedflag = speedflag5;
                    }
                    break;
                case "btnFun6":
                    {
                        frmTop.txtProcessDrivercode(funcmd6, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        spedflag = speedflag6;
                    }
                    break;
                case "btnFun7":
                    {
                        frmTop.txtProcessDrivercode(funcmd7, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        spedflag = speedflag7;
                    }
                    break;
                case "btnFun8":
                    {
                        frmTop.txtProcessDrivercode(funcmd8, out string txtbaseClearOriginal, out string txtMCleatOriginal);
                        spedflag = speedflag8;
                    }
                    break;

            }
            //Setting.TempRecord.cmdLen = 6;
            if (spedflag == "LP")
            {
                frmTop.SendCmd(0);  //低速
            }
            else
            {
                frmTop.SendCmd(4); //高速
            }
            txttip.Text += "命令已下发...\r\n";
            txttip.Select(txttip.Text.Length, 0);
            txttip.ScrollToCaret();
        }

        string FunDefaultfilePath = "";
        string funcmd1, funcmd2, funcmd3, funcmd4, funcmd5, funcmd6, funcmd7, funcmd8;

        private void Gamma51set(object sender, EventArgs e)
        {
            if (rbt_VTDR6110C_Gamma9.Checked) { regSend.Text = "51,0f,ff"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } }
            else if (rbt_VTDR6110C_Gamma1.Checked) { regSend.Text = "51,00,09"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } if (chkAutoTarlum.Checked) txtVT_lum.Text = "" + 0x9 * Convert.ToDouble(DbvMaxlum.Text) / 0x7ff; }
            else if (rbt_VTDR6110C_Gamma2.Checked) { regSend.Text = "51,00,1b"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } if (chkAutoTarlum.Checked) txtVT_lum.Text = "" + 0x1b * Convert.ToDouble(DbvMaxlum.Text) / 0x7ff; }
            else if (rbt_VTDR6110C_Gamma3.Checked) { regSend.Text = "51,00,5b"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } if (chkAutoTarlum.Checked) txtVT_lum.Text = "" + 0x5b * Convert.ToDouble(DbvMaxlum.Text) / 0x7ff; }
            else if (rbt_VTDR6110C_Gamma4.Checked) { regSend.Text = "51,01,11"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } if (chkAutoTarlum.Checked) txtVT_lum.Text = "" + 0x111 * Convert.ToDouble(DbvMaxlum.Text) / 0x7ff; }
            else if (rbt_VTDR6110C_Gamma5.Checked) { regSend.Text = "51,01,f4"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } if (chkAutoTarlum.Checked) txtVT_lum.Text = "" + 0x1f4 * Convert.ToDouble(DbvMaxlum.Text) / 0x7ff; }
            else if (rbt_VTDR6110C_Gamma6.Checked) { regSend.Text = "51,04,00"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } if (chkAutoTarlum.Checked) txtVT_lum.Text = "" + 0x400 * Convert.ToDouble(DbvMaxlum.Text) / 0x7ff; }
            else if (rbt_VTDR6110C_Gamma7.Checked) { regSend.Text = "51,07,ff"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } if (chkAutoTarlum.Checked) txtVT_lum.Text = "" + 0x7ff * Convert.ToDouble(DbvMaxlum.Text) / 0x7ff; }
            else if (rbt_VTDR6110C_Gamma8.Checked) { regSend.Text = "51,08,00"; if (chkAutoSet51.Checked) { VTDR610C_Others_Click(writeReg, e); } }
            btnAutoGammas_VTDR6100.Enabled = true;
            btnIRCGamma.Enabled = true;
        }

        //------------------------------------------------------------
        private void ReadFuncfg(string addr, out String Ftitle, out String speedflag, out String Fcmd)
        {
            lines = new List<string>();

            //FileStream fs = new FileStream(Application.StartupPath + "\\x_ViewTrix\\Fun1.txt", FileMode.Open)
            FileStream fs = new FileStream(addr, FileMode.Open);
            StreamReader rd = new StreamReader(fs);
            string s;
            //读入文件所有行，存放到List<string>集合中
            while ((s = rd.ReadLine()) != null)
            {
                lines.Add(s);
            }
            rd.Close();
            fs.Close();
            //第一行在txttip中显示
            Ftitle = "";
            speedflag = "";
            if (lines.Count > 0)
            {
                Ftitle = lines[0];
                speedflag = lines[1];
            }
            //txtFuncmd.Text = "";
            Fcmd = "";
            for (int i = 2; i < lines.Count; i++)
            {
                Fcmd += lines[i] + "\r\n";
            }

        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            path = Application.StartupPath + "\\x_ViewTrix";
            try
            {
                //StreamReader streamReader = new StreamReader(path+"\\Fun1.txt", Encoding.Default);
                //FunDefaultfilePath = streamReader.ReadLine();
                //streamReader.Close();
                ReadFuncfg(path + "\\Fun1.cfg", out String Ftitle1, out speedflag1, out funcmd1);
                btnFun1.Text = Ftitle1;

                ReadFuncfg(path + "\\Fun2.cfg", out String Ftitle2, out speedflag2, out funcmd2);
                btnFun2.Text = Ftitle2;

                ReadFuncfg(path + "\\Fun3.cfg", out String Ftitle3, out speedflag3, out funcmd3);
                btnFun3.Text = Ftitle3;

                ReadFuncfg(path + "\\Fun4.cfg", out String Ftitle4, out speedflag4, out funcmd4);
                btnFun4.Text = Ftitle4;

                ReadFuncfg(path + "\\Fun5.cfg", out String Ftitle5, out speedflag5, out funcmd5);
                btnFun5.Text = Ftitle5;

                ReadFuncfg(path + "\\Fun6.cfg", out String Ftitle6, out speedflag6, out funcmd6);
                btnFun6.Text = Ftitle6;

                ReadFuncfg(path + "\\Fun7.cfg", out String Ftitle7, out speedflag7, out funcmd7);
                btnFun7.Text = Ftitle7;

                ReadFuncfg(path + "\\Fun8.cfg", out String Ftitle8, out speedflag8, out funcmd8);
                btnFun8.Text = Ftitle8;
                //btnStore.Enabled = true;

                MessageBox.Show("向东说刷新完成", "～～～");
            }
            catch
            {
                MessageBox.Show("向东提示配置文件缺失，请检查", "错误");
            }
        }

        private void VTDR6110C_Gamma_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void btnStore_Click(object sender, EventArgs e)
        {
            //FileStream fs = new FileStream(Application.StartupPath + "\\x_ViewTrix\\Fun1.txt", FileMode.Open)
            //SaveFileDialog txt = new SaveFileDialog();
            //txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            string str = "";
            //if (txt.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(Application.StartupPath + "\\x_ViewTrix\\Fun1.cfg", false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")

                    {
                        str += btnFun1.Text + "\r\n";//
                        str += speedflag1 + "\r\n";
                        str += funcmd1;
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                                // MessageBox.Show("自定义命令保存OK，但请确认文件名是否规范！", "系统提示：");

                    str = ""; sw = null; //文件流
                    sw = new StreamWriter(Application.StartupPath + "\\x_ViewTrix\\Fun2.cfg", false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")

                    {
                        str += btnFun2.Text + "\r\n";//
                        str += speedflag2 + "\r\n";
                        str += funcmd2;
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流

                    str = ""; sw = null; //文件流
                    sw = new StreamWriter(Application.StartupPath + "\\x_ViewTrix\\Fun3.cfg", false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")

                    {
                        str += btnFun3.Text + "\r\n";//
                        str += speedflag3 + "\r\n";
                        str += funcmd3;
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流

                    str = ""; sw = null; //文件流
                    sw = new StreamWriter(Application.StartupPath + "\\x_ViewTrix\\Fun4.cfg", false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")

                    {
                        str += btnFun4.Text + "\r\n";//
                        str += speedflag4 + "\r\n";
                        str += funcmd4;
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流

                    str = ""; sw = null; //文件流
                    sw = new StreamWriter(Application.StartupPath + "\\x_ViewTrix\\Fun5.cfg", false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                    {
                        str += btnFun5.Text + "\r\n";//
                        str += speedflag5 + "\r\n";
                        str += funcmd5;
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流

                    str = ""; sw = null; //文件流
                    sw = new StreamWriter(Application.StartupPath + "\\x_ViewTrix\\Fun6.cfg", false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                    {
                        str += btnFun6.Text + "\r\n";//
                        str += speedflag6 + "\r\n";
                        str += funcmd6;
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流

                    str = ""; sw = null; //文件流
                    sw = new StreamWriter(Application.StartupPath + "\\x_ViewTrix\\Fun7.cfg", false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                    {
                        str += btnFun7.Text + "\r\n";//
                        str += speedflag7 + "\r\n";
                        str += funcmd7;
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流

                    str = ""; sw = null; //文件流
                    sw = new StreamWriter(Application.StartupPath + "\\x_ViewTrix\\Fun8.cfg", false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                    {
                        str += btnFun8.Text + "\r\n";//
                        str += speedflag8 + "\r\n";
                        str += funcmd8;
                    }
                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流

                    MessageBox.Show("向东说固化命令成功！", "系统提示：");
                    btnRefresh.Enabled = true;
                    btnStore.Enabled = false;
                }
                catch
                {
                    MessageBox.Show("固化命令时发生错误，请再试！", "系统提示：");
                }
            }
        }

        private void btnFunTestcmdSend_Click(object sender, EventArgs e)
        {
            String[] lines = txtFuncmd.Text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string speedflag = lines[1];
            String text = "";
            for (int i = 0; i < lines.Length; i++)
            {
                //假设删除第2行第3行
                if (i == 0 || i == 1)
                {
                    continue;
                }
                text += lines[i] + (i == lines.Length - 1 ? "" : "\r\n"); //最后一行不添加换行符
            }
            frmTop.txtProcessDrivercode(text, out string txtbaseClearOriginal, out string txtMCleatOriginal);
            if (speedflag == "LP")
            {
                frmTop.SendCmd(0);  //低速
            }
            else
            {
                frmTop.SendCmd(4); //高速
            }
            txttip.Text += "命令已下发...\r\n";
            txttip.Select(txttip.Text.Length, 0);
            txttip.ScrollToCaret();
        }

        private void btnFunTestcmdSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog txt = new SaveFileDialog();
            txt.Filter = @"txt文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            string str = "";
            if (txt.ShowDialog() == DialogResult.OK)
            {
                if (txt.FileName == " ")
                {
                    return;
                }
                StreamWriter sw = null; //文件流
                sw = new StreamWriter(txt.FileName, false, Encoding.GetEncoding("UTF-8"));//Encoding.GetEncoding("UTF-8")
                try
                {

                    {
                        str += txtFuncmd.Text;// lstMIPIDriver.Items[i].ToString();
                        str += "\r\n";
                    }

                    sw.Write(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("自定义命令保存OK，但请确认文件名是否规范！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        #endregion
        #region     //-----Demura----
        private void Demura_OneKey_Operation_Click(object sender, EventArgs e)
        {

            {
                //   // cbx_write_ram_and_flash_directly.Checked = true;
                //    Application.DoEvents();
                //    Demura_Click(Demura_Flash_Enable, null);
                //    Demura_Click(Flash_Erase_Demura_data, null);
                //    Demura_Click(Inter_Demura_data, null);
                //    Load_Demura_data_Click(null, null);
            }
        }
        public void Demura_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[10];
            Setting.TempRecord.cmdLen = 10;
            Send_buf[0] = 0x2F; //Demura
            Send_buf[2] = 0x05; //Length
                                //Send_buf[7] //byte 7区分机种
                                // if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110C_601)////601
            {
                Send_buf[3] = 0x1D;
            }

            switch (btnTmp.Name)
            {
                case "Demura_Function_ON":
                    {
                        Send_buf[1] = 0x07;
                    }
                    break;
                case "Demura_Function_OFF":
                    {
                        Send_buf[1] = 0x08;
                    }
                    break;

                case "Demura_Flash_Enable":
                    {
                        Send_buf[1] = 0x06;
                    }
                    break;
                case "Flash_Demura_OTP":
                    {
                        Send_buf[1] = 0x09;
                    }
                    break;
                //----------------------------------
                case "Flash_Write_Demura_data2":
                    {
                        Send_buf[1] = 0x0A;
                    }
                    break;
                case "Inter_Demura_data2":
                    {
                        Send_buf[1] = 0x0B;
                    }
                    break;
                case "Flash_Write_Demura_data3":
                    {
                        Send_buf[1] = 0x0C;
                    }
                    break;
                case "Inter_Demura_data3":
                    {
                        Send_buf[1] = 0x0D;
                    }
                    break;
                case "Flash_Write_Demura_data4":
                    {
                        Send_buf[1] = 0x0E;
                    }
                    break;
                case "Inter_Demura_data4":
                    {
                        Send_buf[1] = 0x0F;
                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);
            if (!frmTop.comPort.IsOpen)     //串口功能监测
            {
                try
                {
                    frmTop.comPort.PortName = Detection.GetComNum();
                    frmTop.comPort.BaudRate = Setting.ComSet.comBaudRate;
                    frmTop.openSerialPort(true);
                }
                catch
                {
                    MessageBox.Show("没有搜索到串口！", "错误提示");
                }
            }
            try
            {
                frmTop.comPort.Write(Send_buf, 0, 10);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }
        //private void Load_Demura_data_Click(object sender, EventArgs e)
        //{
        //    Int32 m_txlen = 0;
        //    Int32 Once_txlen = 0;
        //    Int32 File_Length, File_Length_tep;
        //    bool tx_en = true;
        //    byte[] TemFileContent = new byte[1024 * 100];
        //    char[] Acsii_array = new char[2];
        //    Int32 div = 0;

        //    {
        //        div = 180 * 3;
        //        TemFileContent[0] = 0x5B;
        //        TemFileContent[1] = Setting.Demura_Type.VT6110C_601; //30
        //    }

        //    System.Int32 ByteReaded = 0x00;

        //    try
        //    {
        //        string FileName = txtDemuraAddr.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
        //        FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        //        File_Length = Convert.ToInt32(aFile.Length);

        //        try
        //        { //Demura 告诉下位机Demura数据搬运开始
        //            TemFileContent[2] = 0xbd;
        //            TemFileContent[3] = 0xcf;
        //            TemFileContent[4] = 0xfc;
        //            TemFileContent[5] = 0xac;
        //            TemFileContent[6] = 0xca;
        //            TemFileContent[7] = Convert.ToByte(File_Length >> 24);
        //            TemFileContent[8] = Convert.ToByte((File_Length >> 16) & 0xff);
        //            TemFileContent[9] = Convert.ToByte((File_Length >> 8) & 0xff);
        //            TemFileContent[10] = Convert.ToByte(File_Length & 0xff);
        //            frmTop.comPort.Write(TemFileContent, 0, 11);
        //        }
        //        catch
        //        {
        //            MessageBox.Show("串口有误，请检查", "错误");
        //            tx_en = false;
        //            return;
        //        }

        //        File_Length_tep = File_Length;

        //        while (tx_en)
        //        {
        //            if (File_Length_tep > (File_Length % div))
        //            {
        //                Once_txlen = div;
        //            }
        //            else  //最后一笔
        //            {
        //                Once_txlen = Convert.ToInt32(File_Length % div);
        //                tx_en = false;
        //            }
        //            aFile.Seek(0 + m_txlen, SeekOrigin.Begin);    //跳转
        //            ByteReaded = aFile.Read(TemFileContent, 0, Once_txlen); // 存入第0位
        //            try
        //            {
        //                //if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110C_601)
        //                //  DelayMs(1); 
        //                Thread.Sleep(1);
        //                frmTop.comPort.Write(TemFileContent, 0, Once_txlen + 0);
        //                //Thread.Sleep(5);
        //            }
        //            catch
        //            {
        //                MessageBox.Show("串口有误，请检查", "错误");
        //                tx_en = false;
        //                break;
        //            }
        //            //tx_en = false;
        //            m_txlen += ByteReaded;
        //            progressBar2.Value = Convert.ToInt32((double)m_txlen / File_Length * 100);
        //            label_flash.Text = progressBar2.Value.ToString() + "%";
        //            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
        //            File_Length_tep = File_Length_tep - div;
        //        }
        //        //if (cbx_write_ram_and_flash_directly.Checked == true)
        //        {
        //            Demura_Click(Flash_Write_Demura_data, null);
        //        }

        //        {
        //            txttip.Text += "载入完成... \r\n";
        //        }
        //        aFile.Close();
        //    }
        //    catch
        //    {
        //        MessageBox.Show("请选择文件夹", "错误");
        //    }

        //}
        private void Demura_Function_ON_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.Demura_Function_ON, "在此右键，即可DemuraOFF");
        }
        private void Demura_Function_ON_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Demura_Click(Demura_Function_ON, e);
            }
            else
            {
                Demura_Click(Demura_Function_OFF, e);
            }
        }
        private void BIN_load_path(object sender, KeyEventArgs e)       //选择bin/hex文件所在目录
        {
            if (e.KeyCode == Keys.Enter)
            {
                string path = txtDemuraAddr.Text;

                if (!Directory.Exists(path))
                {
                    MessageBox.Show("文件夹不存在.");
                    return;
                }
                listBox3_Demura_File.Items.Clear();
                string s = null;
                DirectoryInfo d = new DirectoryInfo(path);
                FileInfo[] Files = d.GetFiles("*.*");
                // List<string> lstr = new List<string>();
                foreach (FileInfo file in Files)
                {
                    s = file.Name.ToLower();
                    if (s.EndsWith(".hex") || s.EndsWith(".dsc") || s.EndsWith(".bin"))
                    {
                        listBox3_Demura_File.Items.Add(s);
                    }
                }
                txtDemuraAddr.Text = path;
            }
        }
        string defaultfilePath = "";
        private void btnReload_Demura_File_Click(object sender, EventArgs e)    //选择bin/hex文件
        {
            btnOneKey.Enabled = true;
            if (txtDemuraAddr.Text != "")
            {
                defaultfilePath = txtDemuraAddr.Text;
            }
            else
            {
                StreamReader streamReader = new StreamReader("./update/Picaddr.txt", Encoding.Default);
                defaultfilePath = streamReader.ReadLine();
                streamReader.Close();
            }
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;

            if (defaultfilePath != "")
            {                //设置此次默认目录为上一次选中目录
                fldb.SelectedPath = defaultfilePath;
            }

            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件.");
                return;
            }
            string path = fldb.SelectedPath;

            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            listBox3_Demura_File.Items.Clear();
            string s = null;
            DirectoryInfo d = new DirectoryInfo(path);
            //bin文件 识别
            FileInfo[] Files = d.GetFiles(("*.*"));
            foreach (FileInfo file in Files)
            {
                s = file.Name.ToLower();
                if (s.EndsWith(".hex") || s.EndsWith(".dsc") || s.EndsWith(".bin"))
                {
                    listBox3_Demura_File.Items.Add(s);
                }
            }

            txtDemuraAddr.Text = path;
            StreamWriter sw = null; //文件流
            sw = new StreamWriter("./update/Picaddr.txt", false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
            sw.Write(defaultfilePath);
            sw.Dispose(); //释放资源
            sw.Close(); //关闭数据流
        }
        #endregion  //Demura

        //浏览图片
        //string path = "";
        private void btnBrowsePic_Click(object sender, EventArgs e)    //载入画面按键  选择文件夹
        {
            if (txtbmp.Text != "")
            {
                frmTop.defaultfilePath = txtbmp.Text;
            }
            else
            {
                StreamReader streamReader = new StreamReader("./update/Picaddr.txt", Encoding.Default);
                frmTop.defaultfilePath = streamReader.ReadLine();
                streamReader.Close();
            }
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            //首次defaultfilePath为空，按FolderBrowserDialog默认设置（即桌面）选择            
            if (frmTop.defaultfilePath != "")
            {
                //设置此次默认目录为上一次选中目录
                fldb.SelectedPath = frmTop.defaultfilePath;
            }

            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件夹.");
                return;
            }
            frmTop.defaultfilePath = fldb.SelectedPath;
            path = fldb.SelectedPath;
            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            DirectoryInfo di = new DirectoryInfo(path);
            this.listBox3_BMPFile.Items.Clear();
            this.CreateListItem(di);//递归加载文件夹里的所有图片
            txtbmp.Text = path;
            // -------------------------------------------------------------------------
            StreamWriter sw = null; //文件流
            sw = new StreamWriter("./update/Picaddr.txt", false, Encoding.Default);//Encoding.GetEncoding("UTF-8")
            sw.Write(frmTop.defaultfilePath);
            sw.Dispose(); //释放资源
            sw.Close(); //关闭数据流
        }
        /// <summary>
        /// 递归文件夹，把文件夹里所有后缀类似图片的文件，加入listbox
        /// </summary>
        /// <param name="di"></param>
        private void CreateListItem(DirectoryInfo di)
        {
            foreach (DirectoryInfo childDi in di.GetDirectories())
            {
                this.CreateListItem(childDi);
            }

            foreach (FileInfo fi in di.GetFiles())
            {
                if (ImageType.Contains(fi.Extension))//找到一个图片(如果用户手贱把一个excel文件后缀改成jpeg没办法)
                {
                    this.listBox3_BMPFile.Items.Add(new MyImage(fi.FullName));
                }
            }
        }

        private void PMU_OPERATION_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[38];
            Setting.TempRecord.cmdLen = 38; //33+5
            Send_buf[0] = 0x9E;
            Send_buf[1] = 0x05; //lock
            Send_buf[2] = 33; //length
            Send_buf[4] = 0x00;

            switch (btnTmp.Name)
            {
                case "btnPowertest":
                    {
                        Send_buf[3] = 0x00; //oled功耗测量
                    }
                    break;

            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[36], out Send_buf[37]);
            if (!frmTop.comPort.IsOpen)
            {
                try
                {
                    frmTop.comPort.PortName = Detection.GetComNum();
                    frmTop.comPort.BaudRate = Setting.ComSet.comBaudRate;
                    frmTop.openSerialPort(true);
                }
                catch
                {
                    MessageBox.Show("没有搜索到串口！", "错误提示");
                }
            }
            {
                try
                {
                    frmTop.comPort.Write(Send_buf, 0, 38);
                }
                catch
                {
                    //Tim_Powertest.Enabled = false;
                    //chkAutoPowertest.Checked = false;
                    MessageBox.Show("串口有误，请检查", "错误");
                }
                frmTop.T1.Enabled = false;
                frmTop.T1.Interval = 2000;
                frmTop.T1.Enabled = true;
            }
        }
        private float elvddcR, elvddcG, elvddcB;

        private void chkvtdrdircshow_CheckedChanged(object sender, EventArgs e)
        {
            if (chkvtdrdircshow.Checked)
                chkGammaFormatForVTDR.Checked = false;
        }

        private void chkGammaFormatForVTDR_CheckedChanged(object sender, EventArgs e)
        {
            if (chkGammaFormatForVTDR.Checked)
                chkvtdrdircshow.Checked = false;
        }

        private void btnIRCGamma_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[25, 3];
            D_lum = new double[25];
            ircGamma = new double[6, 3];
            index = 0;

            TimTest.Start();
            TimeNow = DateTime.Now; //开始计时

            for (int i = 0; i < 1; i++)
            {
                D_lum[i] = (Math.Pow((Point_VTDR6110C[24 - i] / 255.0), Convert.ToDouble(txtVT_gamma.Text)) * Convert.ToDouble(txtVT_lum.Text));
            }

            D_x = Convert.ToDouble(txtVT_x.Text);
            D_y = Convert.ToDouble(txtVT_y.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 1; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        //lum_off = Convert.ToDouble(txtVT_loff.Text);
                        x_off = Convert.ToDouble(txtVT_xoff.Text);
                        y_off = Convert.ToDouble(txtVT_yoff.Text);
                        OLED_Show_Click(btnwhite, e);
                        ReceivOKFlag = false;
                        T7.Enabled = true;
                    }
                    break;
                case 2:  //寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }
        private double[,] ircGamma;//目标值-单色
        private void T7_Tick(object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            T7.Enabled = false;
            T7.Interval = 200;

            Msr_CA310();

            txtlvshow.Text = "Lv: " + auto_lum + "\r\n" + "x: " + auto_x + "\r\n" + "y: " + auto_y + "\r\n";

            labTargetlum.Text = "Tar:" + D_lum[0];

            Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
            Lb_t = 1.0;
            Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
            lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
            lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
            lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

            lum_off = (D_lum[0] * Convert.ToDouble(numVT_loff.Value) / 1000);
            x_off = Convert.ToDouble(txtVT_xoff.Text);
            y_off = Convert.ToDouble(txtVT_yoff.Text);

            if ((auto_lum < (D_lum[0] + lum_off)) && (auto_lum > (D_lum[0] - lum_off)))//
                flag1 = 0;
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//
                flag2 = 0;
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0)//
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T7.Enabled = false;

                if (index == 4)
                {
                    TimTest.Stop();//停止计时
                    txttip.Text += "IRC gamma调试完毕!\r\n";
                    txttip.Focus();//获取焦点
                    txttip.Select(txttip.TextLength, 0);//光标定位到文本最后
                    txttip.ScrollToCaret();//滚动到光标处
                    chkIRCSendBmp.Checked = false;

                    ircGamma[index, 0] = GammaValue[24, 0];
                    ircGamma[index, 1] = GammaValue[24, 1];
                    ircGamma[index, 2] = GammaValue[24, 2];

                    btnloagGc_Click(null, null);
                    txttip.Text += "------";
                    //FirstAdjGammaFlag = true;
                    return;
                }
                if (index < 5)
                {
                    txttip.Text += index + "" + "完成...";
                    txttip.Select(txttip.Text.Length, 0);
                    txttip.ScrollToCaret();

                    ircGamma[index, 0] = GammaValue[24, 0];
                    ircGamma[index, 1] = GammaValue[24, 1];
                    ircGamma[index, 2] = GammaValue[24, 2];

                    Thread.Sleep(10); //1秒=1000毫秒，
                    index++;

                    if (index == 0)
                    {
                        OLED_Show_Click(btnwhite, e);
                    }
                    else
                    {
                        ircbmpflag = (byte)index;
                        chkIRCSendBmp.Checked = true;
                        btnPrevPic_Click(null, null);
                    }

                    //T7.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }

                ReceivOKFlag = false;
                Thread.Sleep(100);
                T7.Enabled = true;
            }
            else
            {

                {
                    step_l = (byte)(int.Parse(numStep.Value.ToString()));

                    switch (index)
                    {
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                            {
                                if ((flag3 == 1) | (flag2 == 1) | (flag1 == 1))
                                {

                                    {
                                        if (lumr > D_lums[0, 0])
                                            GammaValue[24, 0] = GammaValue[24, 0] - (byte)(int.Parse(numStep.Value.ToString()));
                                        else GammaValue[24, 0] = GammaValue[24, 0] + step_l;
                                        /*if (VTDR6100_R127.Value <= VTDR6100_R111.Value)
                                            VTDR6100_R127.Value = VTDR6100_R111.Value + step_h;*/
                                    }

                                    //}
                                    //if (flag1 == 1)
                                    {

                                        {
                                            if (lumg > D_lums[0, 1])
                                                GammaValue[24, 1] = GammaValue[24, 1] - step_l;
                                            else GammaValue[24, 1] = GammaValue[24, 1] + step_l;
                                            /*if (VTDR6100_G127.Value <= VTDR6100_G111.Value)
                                                VTDR6100_G127.Value = VTDR6100_G111.Value + step_h;*/
                                        }
                                        //adjust_Gnum++;
                                    }
                                    //if (flag2 == 1)
                                    //{

                                    {
                                        if (lumb > D_lums[0, 2])
                                            GammaValue[24, 2] = GammaValue[24, 2] - step_l;
                                        else GammaValue[24, 2] = GammaValue[24, 2] + step_l;
                                        /*if (VTDR6100_B127.Value <= VTDR6100_B111.Value)
                                            VTDR6100_B127.Value = VTDR6100_B111.Value + step_h;*/
                                    }

                                }

                                for (int i = 24; i > 1; i--)
                                {
                                    if (GammaValue[i, 0] < GammaValue[i - 1, 0])
                                    {
                                        GammaValue[i - 1, 0] = GammaValue[i, 0] - 0;
                                        //GammaValueFormat_Click(null, null);
                                    }
                                    if (GammaValue[i, 1] < GammaValue[i - 1, 1])
                                    {
                                        GammaValue[i - 1, 1] = GammaValue[i, 1] - 0;
                                        //GammaValueFormat_Click(null, null);
                                    }
                                    if (GammaValue[i, 2] < GammaValue[i - 1, 2])
                                    {
                                        GammaValue[i - 1, 2] = GammaValue[i, 2] - 0;
                                        //GammaValueFormat_Click(null, null);
                                    }

                                }
                                if (chkAutoSet51.Checked)
                                {
                                    this.GammaReg_dataGridView.Rows.Clear();
                                    for (byte i = 0; i < 25; i++)
                                    {
                                        DataGirdShow(i, GammaValue[i, 0], GammaValue[i, 1], GammaValue[i, 2]);
                                    }
                                }

                                GammaValueFormat_Click(null, null);

                                byte rh = (Byte)(GammaValue[24, 0] >> 8);
                                byte rl = (Byte)(GammaValue[24, 0] & 0xff);
                                byte gh = (Byte)(GammaValue[24, 1] >> 8);
                                byte gl = (Byte)(GammaValue[24, 1] & 0xff);
                                byte bh = (Byte)(GammaValue[24, 2] >> 8);
                                byte bl = (Byte)(GammaValue[24, 2] & 0xff);
                                txtGammaValue.Text = GammaValue[24, 0] + "\r\n" + GammaValue[24, 1] + "\r\n" + GammaValue[24, 2] + "\r\n";
                                GammaValueSet(rh, rl, gh, gl, bh, bl, GammaValueTeparr);
                                Thread.Sleep(100);
                                Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                                //SendGamma2VTDR();
                                //Thread.Sleep(100);
                            }
                            break;


                        default:
                            T7.Enabled = false;
                            break;
                    }
                    System.Threading.Thread.Sleep(10);
                }
                //catch
                //{
                //    T5.Enabled = false;
                //    MessageBox.Show("数值超限！");
                //    return;
                // }
                T7.Enabled = true;
            }

        }

        private void btnloagGc_Click(object sender, EventArgs e)
        {
            for (int t = 0; t < 4; t++)
            {
                dataGridView_BestGC.Rows[t].Cells[0 + 1].Value = ircGamma[t + 1, 0];
                dataGridView_BestGC.Rows[t].Cells[0 + 2].Value = ircGamma[t + 1, 1];
                dataGridView_BestGC.Rows[t].Cells[0 + 3].Value = ircGamma[t + 1, 2];
            }
            dataGridView_BestGC.Rows[4].Cells[0 + 1].Value = ircGamma[0, 0];
            dataGridView_BestGC.Rows[4].Cells[0 + 2].Value = ircGamma[0, 1];
            dataGridView_BestGC.Rows[4].Cells[0 + 3].Value = ircGamma[0, 2];
        }


        private void btnReadGamma7to1_Click(object sender, EventArgs e)
        {
            chkvtdrdircshow.Checked = true;
            richTextBox_GC_In.Text = "";

            rbt_VTDR6110C_Gamma7.PerformClick();
            Thread.Sleep(500);
            VTDR6110C_GammaR.PerformClick();
            Thread.Sleep(1000);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            btnSaveGamma.PerformClick();

            rbt_VTDR6110C_Gamma6.PerformClick();
            Thread.Sleep(500);
            VTDR6110C_GammaR.PerformClick();
            Thread.Sleep(1000);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            btnSaveGamma.PerformClick();

            rbt_VTDR6110C_Gamma5.PerformClick();
            Thread.Sleep(500);
            VTDR6110C_GammaR.PerformClick();
            Thread.Sleep(1000);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            btnSaveGamma.PerformClick();

            rbt_VTDR6110C_Gamma4.PerformClick();
            Thread.Sleep(500);
            VTDR6110C_GammaR.PerformClick();
            Thread.Sleep(1000);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            btnSaveGamma.PerformClick();

            rbt_VTDR6110C_Gamma3.PerformClick();
            Thread.Sleep(500);
            VTDR6110C_GammaR.PerformClick();
            Thread.Sleep(1000);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            btnSaveGamma.PerformClick();

            rbt_VTDR6110C_Gamma2.PerformClick();
            Thread.Sleep(500);
            VTDR6110C_GammaR.PerformClick();
            Thread.Sleep(1000);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            btnSaveGamma.PerformClick();

            rbt_VTDR6110C_Gamma1.PerformClick();
            Thread.Sleep(500);
            VTDR6110C_GammaR.PerformClick();
            Thread.Sleep(1000);
            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            btnSaveGamma.PerformClick();
        }

        private void btnCleanTextBox_Click(object sender, EventArgs e)
        {
            txttip.Enabled = true;
            txttip.Clear();
        }

        private void btnData2DRAM_Click(object sender, EventArgs e)
        {
            //private void PreLoad_Demura_data_Click()  //预加载Demura文件//private void PreLoad_Demura_data_Click(object sender, EventArgs e)  //预加载Demura文件
            {
                Int32 m_txlen = 0;
                Int32 Once_txlen = 0;
                Int32 File_Length, File_Length_tep;
                bool tx_en = true;
                byte[] TemFileContent = new byte[1024 * 100];
                char[] Acsii_array = new char[2];
                Int32 div = 0;

                div = Convert.ToInt16(frmTop.HACT.Value) * 3;
                System.Int32 ByteReaded = 0x00;
                try
                {
                    string FileName = txtDemuraAddr.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
                    FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    File_Length = Convert.ToInt32(aFile.Length);
                    File_Length_tep = File_Length;
                    //------------------------------------------
                    textBox1.Text += "设定 Demura Flag \r\n";
                    try
                    { //Demura 告诉下位机Demura数据搬运开始
                        TemFileContent[0] = 0xbd;
                        TemFileContent[1] = 0x5B;
                        TemFileContent[2] = 0xb5;
                        TemFileContent[3] = 0xd9;
                        TemFileContent[4] = 0x9d;
                        TemFileContent[5] = Convert.ToByte(File_Length >> 24);
                        TemFileContent[6] = Convert.ToByte((File_Length >> 16) & 0xff);
                        TemFileContent[7] = Convert.ToByte((File_Length >> 8) & 0xff);
                        TemFileContent[8] = Convert.ToByte(File_Length & 0xff);
                        frmTop.comPort.Write(TemFileContent, 0, 9);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        tx_en = false;
                        return;
                    }
                    //------------------------------------------
                    while (tx_en)
                    {
                        if (File_Length_tep > (File_Length % div))
                        {
                            Once_txlen = div;
                        }
                        else  //最后一笔
                        {
                            Once_txlen = Convert.ToInt32(aFile.Length % div);
                            tx_en = false;
                        }
                        aFile.Seek(0 + m_txlen, SeekOrigin.Begin);    //跳转
                        ByteReaded = aFile.Read(TemFileContent, 0, Once_txlen); // 
                        try
                        {
                            frmTop.comPort.Write(TemFileContent, 0, ByteReaded);
                            //Thread.Sleep(2);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            tx_en = false;
                            break;
                        }
                        //tx_en = false;
                        m_txlen += ByteReaded;
                        progressBar2.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        label_flash.Text = progressBar2.Value.ToString() + "%";
                        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        File_Length_tep = File_Length_tep - div;
                    }
                    aFile.Close();
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }
        }


        private void btnErase_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txtErase.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
        }

        private void btnDemuraflashParsSet_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txtDemuraflashParsSet.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
        }

        private void btnstartProgram_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txtstartProgram.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;

        }

        private void btnprogramEnd_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = txtprogramEnd.Text;
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
        }

        private void numStep_ValueChanged(object sender, EventArgs e)
        {
            rstep = (byte)(int.Parse(numStep.Value.ToString()));
            gstep = (byte)(int.Parse(numStep.Value.ToString()));
            bstep = (byte)(int.Parse(numStep.Value.ToString()));
        }

        private void Demura_Function_ON_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = "59,09";
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
        }

        private void Demura_Function_OFF_Click(object sender, EventArgs e)
        {
            string tepstr = "";
            tepstr = txtCmd3.Text;
            txtCmd3.Text = "59,00";
            Send_Cmd_Click(btnSend3, e);
            txtCmd3.Text = tepstr;
        }

        private void btnOneKey_Click(object sender, EventArgs e)
        {
            txttip.Text = "请等待Demura完成信息提示...";
            btnErase.PerformClick();
            Thread.Sleep(11000);
            btnDemuraflashParsSet.PerformClick();
            btnData2DRAM.PerformClick();
            Thread.Sleep(1000);
            btnstartProgram.PerformClick();
            Thread.Sleep(12000);
            MessageBox.Show("Demura 完成", "提示");
            // btnprogramEnd.PerformClick();
        }

        private void rdodbvset1_CheckedChanged(object sender, EventArgs e)
        {
            dataGridView_GC_DBV.Rows.Clear();
            if (rdodbvset1.Checked)
            {
                dataGridView_GC_DBV.Rows.Add("GC_Set1", "3", "2047");
                dataGridView_GC_DBV.Rows.Add("GC_Set2", "1", "2047");
                dataGridView_GC_DBV.Rows.Add("GC_Set3", "3", "2047");
                dataGridView_GC_DBV.Rows.Add("GC_Set4", "1", "1024");
                dataGridView_GC_DBV.Rows.Add("GC_Set5", "3", "500");
                dataGridView_GC_DBV.Rows.Add("GC_Set6", "1", "273");
                dataGridView_GC_DBV.Rows.Add("GC_Set7", "1", "91");
                dataGridView_GC_DBV.Rows.Add("GC_Set8", "1", "27");
                dataGridView_GC_DBV.Rows.Add("GC_Set9", "1", "9");
                dataGridView_GC_DBV.Rows.Add("GC_Set10", "0", "0");
                dataGridView_GC_DBV.Rows.Add("GC_Set11", "0", "0");
                dataGridView_GC_DBV.Rows.Add("GC_Set12", "0", "0");
            }
            else
            {
                dataGridView_GC_DBV.Rows.Add("GC_Set1", "3", "2047");
                dataGridView_GC_DBV.Rows.Add("GC_Set2", "1", "2047");
                dataGridView_GC_DBV.Rows.Add("GC_Set3", "3", "2047");
                dataGridView_GC_DBV.Rows.Add("GC_Set4", "1", "1024");
                dataGridView_GC_DBV.Rows.Add("GC_Set5", "0", "500");
                dataGridView_GC_DBV.Rows.Add("GC_Set6", "0", "273");
                dataGridView_GC_DBV.Rows.Add("GC_Set7", "0", "91");
                dataGridView_GC_DBV.Rows.Add("GC_Set8", "3", "27");
                dataGridView_GC_DBV.Rows.Add("GC_Set9", "1", "9");
                dataGridView_GC_DBV.Rows.Add("GC_Set10", "0", "0");
                dataGridView_GC_DBV.Rows.Add("GC_Set11", "0", "0");
                dataGridView_GC_DBV.Rows.Add("GC_Set12", "0", "0");
            }

        }

        private void btnLoadGC_Click(object sender, EventArgs e)
        {
            string str1 = "";
            string smask = "\r\n";
            str1 = txttip.Text;
            str1 = str1.Replace(smask, ",");
            str1 = str1.Replace('\t', ',');
            string[] strA;
            strA = str1.Trim().Split(',');
            try
            {
                for (int t = 0; t < 5; t++)
                {
                    dataGridView_BestGC.Rows[t].Cells[0 + 1].Value = Convert.ToInt32(strA[t * 3 + 1 - 1]);
                    dataGridView_BestGC.Rows[t].Cells[0 + 2].Value = Convert.ToInt32(strA[t * 3 + 2 - 1]);
                    dataGridView_BestGC.Rows[t].Cells[0 + 3].Value = Convert.ToInt32(strA[t * 3 + 3 - 1]);
                }
            }
            catch
            {
                MessageBox.Show("GC加载失败", "错误");
                return;
            }
        }

        private int regelvddcR, regelvddcG, regelvddcB;
        private void btnELVDDRGBCalc_Click(object sender, EventArgs e)
        {
            float tep; int i;
            tep = elvddcR + elvddcG + elvddcB;
            regelvddcR = (int)(0.5 + 256 * elvddcR / tep);
            regelvddcG = (int)(0.5 + 256 * elvddcG / tep);
            //regelvddcB = (int)(256 * elvddcB / tep);
            regelvddcB = 256 - regelvddcR - regelvddcG;
            txttip.Text += regelvddcR + "\r\n" + regelvddcG + "\r\n" + regelvddcB + "\r\n";
            i = 3;
            {
                // Reg_IRC.Para[i] = Convert.ToInt16(dataGridView_Para_In.Rows[4].Cells[1].Value);
                dataGridView_Para_In.Rows[i].Cells[1].Value = regelvddcR + ""; i++;
                dataGridView_Para_In.Rows[i].Cells[1].Value = regelvddcG + ""; i++;
                dataGridView_Para_In.Rows[i].Cells[1].Value = regelvddcB + "";
            }
        }

        private void btnELVDD_R_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;


            switch (btnTmp.Name)
            {
                case "btnELVDD_R":
                    {
                        OLED_Show_Click(btnred, e); Thread.Sleep(300);
                        PMU_OPERATION_Click(btnPowertest, e);
                        elvddcR = ELVDDC;
                    }
                    break;
                case "btnELVDD_G":
                    {
                        OLED_Show_Click(btngreen, e); Thread.Sleep(300);
                        PMU_OPERATION_Click(btnPowertest, e);

                        elvddcG = ELVDDC;
                    }
                    break;
                case "btnELVDD_B":
                    {
                        OLED_Show_Click(btnblue, e); Thread.Sleep(300);
                        PMU_OPERATION_Click(btnPowertest, e);

                        elvddcB = ELVDDC;
                    }
                    break;
            }


        }

        private void lstImage_Click(object sender, EventArgs e)
        {
            if (listBox3_BMPFile.SelectedItem == null)
            {
                return;
            }
            MyImage selectedImg = (MyImage)listBox3_BMPFile.SelectedItem;
            Image img = new Bitmap(selectedImg.Path);
            pictureBox1.Image = img;
        }
        private byte ircbmpflag = 1;
        private void btnPrevPic_Click(object sender, EventArgs e)
        {
            Int32 HACT_True = 0;
            Int32 HACT_Skip = 0;
            Int32 VACT_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            FileStream aFile;
            string FileName = "";
            //byte[] TemFileContent = new byte[1024 * 100];
            byte[] TemFileContent = new byte[512 * 4000 + 100];
            System.Int32 ByteReaded = 0x00;
            if (chkIRCSendBmp.Checked)
            {
                if (ircbmpflag == 1)
                    FileName = txtbmp.Text + '\\' + "p16.bmp";
                else if (ircbmpflag == 2)
                    FileName = txtbmp.Text + '\\' + "p33.bmp";
                else if (ircbmpflag == 3)
                    FileName = txtbmp.Text + '\\' + "p50.bmp";
                else if (ircbmpflag == 4)
                    FileName = txtbmp.Text + '\\' + "p75.bmp";
                else
                {
                    OLED_Show_Click(btnwhite, e);
                }
            }
            else
            {
                FileName = txtbmp.Text + '\\' + listBox3_BMPFile.SelectedItem.ToString();
            }
            aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            ByteReaded = aFile.Read(TemFileContent, 0, 54);

            HACT_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            VACT_True = ((TemFileContent[23] << 8) + TemFileContent[22]);

            //   if (HACT.Value != HACT_True)
            //    {
            //          txttip.Text += "图片宽度与设定不符\r\n";
            //         return;
            //     }
            /*
            if ((BridgeChipModel.SelectedIndex == 1) && (Dis_mode.SelectedIndex == 1)) //(Dis_mode.SelectedIndex == 1):MIPI 模式，因为下位机仅对MIPI模式做了优化
            {
                //------------20180903---------------
                try
                {   //bd 5a a5 9DF0-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    //BD A5 5A 9DF0-----提前下个标志位告诉下位机开始 快速 送图了
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0xa5; TemFileContent[2] = 0x5a; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {

                    j = 0;
                    int x, y, s = 0;
                    x = 512 * 4000; //单次传输数量
                    y = (VACT_True * HACT_True * 3 / x); //传输次数
                    if ((VACT_True * HACT_True * 3 % x) != 0)
                        y++;
                    {
                        //aFile.Seek(54, SeekOrigin.Begin);
                        while (j < y)
                        {
                            //if (HACT_True % 4 != 0)
                            //    aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            //else
                            //    aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);
                            aFile.Seek((j) * x + 54, SeekOrigin.Begin);

                            ByteReaded = aFile.Read(TemFileContent, 0, x);

                            try
                            {
                                comPort.Write(TemFileContent, 0, ByteReaded);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }

                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label3.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();
                            j++;
                        }

                        TemFileContent[0] = 0x5A;
                        try
                        {
                            comPort.Write(TemFileContent, 0, 1);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                        }

                        aFile.Close();
                    }
                }
                catch
                {
                    MessageBox.Show("请选择文件夹", "错误");
                }
            }
            else*/
            {
                if ((frmTop.Dis_mode.SelectedIndex == 1) && (Pixel_Offset_Enable.Checked == true)) //video
                {
                    HACT_Skip = HACT_True % 10 > 0 ? (HACT_True + 10 - HACT_True % 10) : HACT_True;
                }
                else
                {
                    HACT_Skip = HACT_True;
                }
                try
                { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                    TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                    frmTop.comPort.Write(TemFileContent, 0, 5);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                //-----------------------------------
                try
                {
                    //HACT_True = Sig_mode.SelectedIndex > 2 ? Convert.ToInt32(2 * ((TemFileContent[19]<<8)+ TemFileContent[18])) : Convert.ToInt32((TemFileContent[19] << 8) + TemFileContent[18]);
                    j = 1;
                    while (j <= VACT_True)
                    {
                        //if (Sig_mode.SelectedIndex == 2)
                        //{
                        //    aFile.Seek((VACT_True - j) * HACT_True * 2 + 66, SeekOrigin.Begin);
                        //    ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 2);
                        //    try
                        //    {
                        //        frmTop.comPort.Write(TemFileContent, 0, HACT_Skip * 2);
                        //    }
                        //    catch
                        //    {
                        //        MessageBox.Show("串口有误，请检查", "错误");
                        //        break;
                        //    }
                        //    m_txlen += ByteReaded;
                        //    progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                        //    label3.Text = progressBar1.Value.ToString() + "%";
                        //    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                        //    j++;
                        //}
                        //else
                        {
                            if (HACT_True % 4 != 0)
                                aFile.Seek((VACT_True - j) * (HACT_True * 3 + (HACT_True % 4)) + 54, SeekOrigin.Begin);
                            else
                                aFile.Seek((VACT_True - j) * HACT_True * 3 + 54, SeekOrigin.Begin);

                            ByteReaded = aFile.Read(TemFileContent, 0, HACT_True * 3); // 
                            try
                            {
                                frmTop.comPort.Write(TemFileContent, 0, HACT_Skip * 3);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                                break;
                            }
                            m_txlen += ByteReaded;
                            progressBar1.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 100);
                            label3.Text = progressBar1.Value.ToString() + "%";
                            Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                            j++;
                        }
                    }

                    TemFileContent[0] = 0x5A;
                    try
                    {
                        frmTop.comPort.Write(TemFileContent, 0, 1);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        return;
                    }
                    aFile.Close();
                }
                catch
                {
                    txttip.Text += "啥图片都没显示";
                    txttip.Select(txttip.Text.Length, 0);
                    txttip.ScrollToCaret();
                }
            }
        }
        public class MyImage
        {
            public MyImage(string p)
            {
                this.Path = p;
            }
            public string Path { get; set; }
            public override string ToString()
            {
                if (Path == null)
                {
                    return null;
                }
                return System.IO.Path.GetFileName(this.Path);
            }
        }
        //-----------------------------------------

        public struct IRC_Input
        {
            public int[] Para;//In_Para
            public int[,] Ratio_V255_GC;//[RGB/OPR 16.91,33.33,50,75,100]
            public int[] DBV;
            public int[] DBV_Mode;//0:Normal 1:DLL 2:OffSet 3:Base
            public int[,,] IRC_GC_In;//[Set/RGB/GC Code]

            public string[] IRC_Msg;
        }
        public struct IRC_Output
        {
            public int[,] Reg_Out;//[CMD,Para]
            public int[,,] IRC_GC_Out;//[Set/RGB/GC Code]
        }
        //==============================================================
        IRC_Input Reg_IRC = new IRC_Input();
        IRC_Output Reg_Result_IRC = new IRC_Output();
        //==============================================================
        private void btnLoadDefault_Click(object sender, EventArgs e)
        {
            this.dataGridView_Para_In.Rows.Clear();
            this.dataGridView_BestGC.Rows.Clear();
            this.dataGridView_GC_DBV.Rows.Clear();
            interface_Param();
            IRC_Initialize();
        }

        public void interface_Param()
        {
            Reg_IRC.Para = new int[9] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            Reg_IRC.Ratio_V255_GC = new int[3, 5];
            Reg_IRC.DBV = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            Reg_IRC.DBV_Mode = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            Reg_IRC.IRC_GC_In = new int[10, 3, 25];
            Reg_Result_IRC.IRC_GC_Out = new int[10, 3, 25];
            Reg_Result_IRC.Reg_Out = new int[2, 30];

            Array.Clear(Reg_IRC.Para, 0, Reg_IRC.Para.Length);
            Array.Clear(Reg_IRC.Ratio_V255_GC, 0, Reg_IRC.Ratio_V255_GC.Length);
            Array.Clear(Reg_IRC.DBV, 0, Reg_IRC.DBV.Length);
            Array.Clear(Reg_IRC.DBV_Mode, 0, Reg_IRC.DBV_Mode.Length);
            Array.Clear(Reg_IRC.IRC_GC_In, 0, Reg_IRC.IRC_GC_In.Length);
            Array.Clear(Reg_Result_IRC.IRC_GC_Out, 0, Reg_Result_IRC.IRC_GC_Out.Length);
            Array.Clear(Reg_Result_IRC.Reg_Out, 0, Reg_Result_IRC.Reg_Out.Length);
        }

        public void IRC_Initialize()
        {
            dataGridView_Para_In.Rows.Add("LHB_EN", "0");
            dataGridView_Para_In.Rows.Add("DBV_EN", "1");
            dataGridView_Para_In.Rows.Add("HBM_EN", "0");
            dataGridView_Para_In.Rows.Add("Weight_R", "88");
            dataGridView_Para_In.Rows.Add("Weight_G", "70");
            dataGridView_Para_In.Rows.Add("Weight_B", "98");
            dataGridView_Para_In.Rows.Add("Max DBV", "2047");
            dataGridView_Para_In.Rows.Add("Internal_A", "2");
            dataGridView_Para_In.Rows.Add("Internal_B", "2");

            dataGridView_BestGC.Rows.Add("16.91%", "823", "788", "871");
            dataGridView_BestGC.Rows.Add("33.33%", "837", "801", "883");
            dataGridView_BestGC.Rows.Add("50%", "845", "809", "891");
            dataGridView_BestGC.Rows.Add("75%", "860", "824", "906");
            dataGridView_BestGC.Rows.Add("100%", "872", "836", "921");

            rdodbvset1_CheckedChanged(null, null);

            richTextBox_GC_In.Text += "//Gamma set4\r\n";
            richTextBox_GC_In.Text += "//DBV=  2047  \r\n";
            richTextBox_GC_In.Text += " BF ,03\r\n";
            richTextBox_GC_In.Text += " B0 , 00 , 00 , 00 , 4D , 00 , 7A , 00 , 98 , 00 , AE , 00 , D3 , 00 , F0 , 01 , 09\r\n";
            richTextBox_GC_In.Text += " B1 , 01 , 1F , 01 , 30 , 01 , 40 , 01 , 50 , 01 , 5E , 01 , 76 , 01 , 8C , 01 , 9D\r\n";
            richTextBox_GC_In.Text += " B2 , 01 , AE , 01 , BF , 01 , E1 , 01 , FA , 02 , 11 , 02 , 3C , 02 , 67 , 02 , 92\r\n";
            richTextBox_GC_In.Text += " B3 , 02 , BA\r\n";
            richTextBox_GC_In.Text += " B4 , 00 , 00 , 00 , 3F , 00 , 66 , 00 , 7F , 00 , 92 , 00 , B2 , 00 , CB , 00 , E1\r\n";
            richTextBox_GC_In.Text += " B5 , 00 , F4 , 01 , 05 , 01 , 15 , 01 , 26 , 01 , 34 , 01 , 4C , 01 , 63 , 01 , 74\r\n";
            richTextBox_GC_In.Text += " B6 , 01 , 86 , 01 , 97 , 01 , BA , 01 , D4 , 01 , EA , 02 , 17 , 02 , 43 , 02 , 6F\r\n";
            richTextBox_GC_In.Text += " B7 , 02 , 98\r\n";
            richTextBox_GC_In.Text += " B8 , 00 , 00 , 00 , 48 , 00 , 76 , 00 , 94 , 00 , AC , 00 , D3 , 00 , F2 , 01 , 0D\r\n";
            richTextBox_GC_In.Text += " B9 , 01 , 25 , 01 , 37 , 01 , 46 , 01 , 55 , 01 , 62 , 01 , 78 , 01 , 8C , 01 , 9D\r\n";
            richTextBox_GC_In.Text += " BA , 01 , AE , 01 , C0 , 01 , E4 , 02 , 00 , 02 , 18 , 02 , 48 , 02 , 78 , 02 , A9\r\n";
            richTextBox_GC_In.Text += " BB , 02 , D7\r\n\r\n";

            richTextBox_GC_In.Text += "//Gamma set3\r\n";
            richTextBox_GC_In.Text += "//DBV=  ???  \r\n";
            richTextBox_GC_In.Text += " BF ,02\r\n";
            richTextBox_GC_In.Text += " B0 , 00 , 00 , 00 , 51 , 00 , 81 , 00 , A0 , 00 , B9 , 00 , E1 , 01 , 01 , 01 , 1C\r\n";
            richTextBox_GC_In.Text += " B1 , 01 , 32 , 01 , 45 , 01 , 58 , 01 , 67 , 01 , 76 , 01 , 91 , 01 , A8 , 01 , BE\r\n";
            richTextBox_GC_In.Text += " B2 , 01 , D1 , 01 , E4 , 02 , 05 , 02 , 22 , 02 , 3C , 02 , 71 , 02 , A2 , 02 , CE\r\n";
            richTextBox_GC_In.Text += " B3 , 02 , FB\r\n";
            richTextBox_GC_In.Text += " B4 , 00 , 00 , 00 , 40 , 00 , 69 , 00 , 84 , 00 , 99 , 00 , BC , 00 , D9 , 00 , F1\r\n";
            richTextBox_GC_In.Text += " B5 , 01 , 05 , 01 , 19 , 01 , 2C , 01 , 3B , 01 , 4A , 01 , 66 , 01 , 7E , 01 , 95\r\n";
            richTextBox_GC_In.Text += " B6 , 01 , A8 , 01 , BB , 01 , DD , 01 , FC , 02 , 16 , 02 , 4C , 02 , 7F , 02 , AC\r\n";
            richTextBox_GC_In.Text += " B7 , 02 , D9\r\n";
            richTextBox_GC_In.Text += " B8 , 00 , 00 , 00 , 4B , 00 , 7C , 00 , 9D , 00 , B7 , 00 , E2 , 01 , 04 , 01 , 22\r\n";
            richTextBox_GC_In.Text += " B9 , 01 , 38 , 01 , 4A , 01 , 5B , 01 , 69 , 01 , 76 , 01 , 8F , 01 , A7 , 01 , BE\r\n";
            richTextBox_GC_In.Text += " BA , 01 , D3 , 01 , E8 , 02 , 0B , 02 , 2C , 02 , 49 , 02 , 84 , 02 , BB , 02 , ED\r\n";
            richTextBox_GC_In.Text += " BB , 03 , 20\r\n\r\n";

            richTextBox_GC_In.Text += "//Gamma set2\r\n";
            richTextBox_GC_In.Text += "//DBV=  ???  \r\n";
            richTextBox_GC_In.Text += " BF ,01\r\n";
            richTextBox_GC_In.Text += " B0 , 00 , 00 , 00 , 57 , 00 , 8A , 00 , AB , 00 , C5 , 00 , EE , 01 , 0E , 01 , 2A\r\n";
            richTextBox_GC_In.Text += " B1 , 01 , 3F , 01 , 54 , 01 , 65 , 01 , 75 , 01 , 85 , 01 , 9C , 01 , B3 , 01 , C9\r\n";
            richTextBox_GC_In.Text += " B2 , 01 , E0 , 01 , F3 , 02 , 10 , 02 , 2D , 02 , 4A , 02 , 83 , 02 , BA , 02 , EB\r\n";
            richTextBox_GC_In.Text += " B3 , 03 , 1C\r\n";
            richTextBox_GC_In.Text += " B4 , 00 , 00 , 00 , 48 , 00 , 73 , 00 , 8F , 00 , A6 , 00 , C9 , 00 , E6 , 00 , FE\r\n";
            richTextBox_GC_In.Text += " B5 , 01 , 14 , 01 , 2A , 01 , 3B , 01 , 4B , 01 , 5C , 01 , 74 , 01 , 8B , 01 , A2\r\n";
            richTextBox_GC_In.Text += " B6 , 01 , B9 , 01 , CC , 01 , EA , 02 , 08 , 02 , 25 , 02 , 60 , 02 , 98 , 02 , C9\r\n";
            richTextBox_GC_In.Text += " B7 , 02 , FA\r\n";
            richTextBox_GC_In.Text += " B8 , 00 , 00 , 00 , 52 , 00 , 86 , 00 , A8 , 00 , C4 , 00 , F0 , 01 , 13 , 01 , 30\r\n";
            richTextBox_GC_In.Text += " B9 , 01 , 45 , 01 , 59 , 01 , 68 , 01 , 77 , 01 , 85 , 01 , 9C , 01 , B3 , 01 , CB\r\n";
            richTextBox_GC_In.Text += " BA , 01 , E4 , 01 , F7 , 02 , 18 , 02 , 38 , 02 , 58 , 02 , 98 , 02 , D6 , 03 , 0D\r\n";
            richTextBox_GC_In.Text += " BB , 03 , 45\r\n\r\n";

            richTextBox_GC_In.Text += "//Gamma set1\r\n";
            richTextBox_GC_In.Text += "//DBV=  ???  \r\n";
            richTextBox_GC_In.Text += " BF ,00\r\n";
            richTextBox_GC_In.Text += " B0 , 00 , 00 , 00 , 5E , 00 , 94 , 00 , B8 , 00 , D3 , 00 , FF , 01 , 23 , 01 , 3D\r\n";
            richTextBox_GC_In.Text += " B1 , 01 , 52 , 01 , 68 , 01 , 7C , 01 , 8D , 01 , 9C , 01 , B6 , 01 , D0 , 01 , EB\r\n";
            richTextBox_GC_In.Text += " B2 , 01 , FF , 02 , 12 , 02 , 37 , 02 , 59 , 02 , 7B , 02 , B7 , 02 , F1 , 03 , 2C\r\n";
            richTextBox_GC_In.Text += " B3 , 03 , 68\r\n";
            richTextBox_GC_In.Text += " B4 , 00 , 00 , 00 , 4D , 00 , 7C , 00 , 9A , 00 , B3 , 00 , D9 , 00 , F8 , 01 , 12\r\n";
            richTextBox_GC_In.Text += " B5 , 01 , 27 , 01 , 3E , 01 , 53 , 01 , 63 , 01 , 73 , 01 , 8E , 01 , A9 , 01 , C4\r\n";
            richTextBox_GC_In.Text += " B6 , 01 , D8 , 01 , EC , 02 , 12 , 02 , 35 , 02 , 58 , 02 , 95 , 02 , CF , 03 , 09\r\n";
            richTextBox_GC_In.Text += " B7 , 03 , 44\r\n";
            richTextBox_GC_In.Text += " B8 , 00 , 00 , 00 , 59 , 00 , 91 , 00 , B6 , 00 , D4 , 01 , 03 , 01 , 2A , 01 , 43\r\n";
            richTextBox_GC_In.Text += " B9 , 01 , 55 , 01 , 68 , 01 , 7B , 01 , 8A , 01 , 9A , 01 , B6 , 01 , D2 , 01 , EF\r\n";
            richTextBox_GC_In.Text += " BA , 02 , 06 , 02 , 1C , 02 , 45 , 02 , 6B , 02 , 90 , 02 , D3 , 03 , 14 , 03 , 56\r\n";
            richTextBox_GC_In.Text += " BB , 03 , 99\r\n\r\n";

        }
        private void IRC_Reg_Ini()
        {
            int i, RGB;

            for (i = 0; i < 9; i++)    //获取 DBV 设定以及 ELVSS /ELVDD 电流比例等参数
            {
                Reg_IRC.Para[i] = Convert.ToInt16(dataGridView_Para_In.Rows[i].Cells[1].Value);
            }

            for (RGB = 0; RGB < 3; RGB++)   //获取5组不同画面下的Gamma R/G/G 寄存器值
            {
                for (i = 0; i < 5; i++)
                {
                    Reg_IRC.Ratio_V255_GC[RGB, i] = Convert.ToInt16(dataGridView_BestGC.Rows[i].Cells[RGB + 1].Value);
                }
            }
            //Reg_IRC.Max_DBV = Convert.ToInt16(dataGridView_GC_DBV.Rows[0].Cells[2].Value);
            for (i = 0; i < 12; i++)   //获取DBV 设定
            {
                Reg_IRC.DBV[i] = Convert.ToInt16(dataGridView_GC_DBV.Rows[i].Cells[2].Value);
                Reg_IRC.DBV_Mode[i] = Convert.ToInt16(dataGridView_GC_DBV.Rows[i].Cells[1].Value);
            }
        }
        private void GC_In()
        {
            string[] list;
            int i, j, GC_set, GC_Count, GC_Dec, RGB_Count, Regw_Flag;
            string line, CMD, GC_Temp;

            Reg_IRC.IRC_Msg = new string[richTextBox_GC_In.Lines.Length];

            GC_set = 0;
            GC_Count = 0;
            RGB_Count = 0;
            Regw_Flag = 1;//0:Have "Regw" 1:No "Regw"

            for (i = 0; i < richTextBox_GC_In.Lines.Length; i++)
            {
                Reg_IRC.IRC_Msg[i] = richTextBox_GC_In.Lines[i];
                line = richTextBox_GC_In.Lines[i];
                line = line.Replace(" ", "");
                list = line.Trim().Split(',');

                if (list[0] == "")
                {
                    continue;
                }
                //else if ((list[0] == "REGW") || (list[0] == "regw") || (list[0].Substring(0, 2) == "0x"))
                {
                    if (list[0].Substring(0, 2) == "0x")
                        Regw_Flag = 1;
                    else
                        Regw_Flag = 0;


                    CMD = list[1 - 1].Substring(0, 2);//取 CMD

                    if ((CMD == "B0") || (CMD == "B1") || (CMD == "B2") || (CMD == "B3"))
                    {
                        RGB_Count = 0;
                    }
                    else if ((CMD == "B4") || (CMD == "B5") || (CMD == "B6") || (CMD == "B7"))
                    {
                        RGB_Count = 1;
                    }
                    else if ((CMD == "B8") || (CMD == "B9") || (CMD == "BA") || (CMD == "BB"))
                    {
                        RGB_Count = 2;
                    }
                    else
                    {
                        continue;
                    }

                    list = line.Trim().Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                    if ((CMD == "B3") || (CMD == "B7") || (CMD == "BB"))
                    {
                        GC_Temp = list[2 - 1].Substring(0, 2) + list[3 - 1].Substring(0, 2);
                        GC_Dec = Int32.Parse(GC_Temp, System.Globalization.NumberStyles.HexNumber);
                        Reg_IRC.IRC_GC_In[GC_set, RGB_Count, GC_Count] = GC_Dec;

                        GC_Count = 0;
                        if (CMD == "BB")
                            GC_set = GC_set + 1;
                    }
                    else
                    {
                        for (j = 0; j < 8; j++)
                        {
                            GC_Temp = list[2 - 1 + j * 2].Substring(0, 2) + list[3 - 1 + j * 2].Substring(0, 2);
                            GC_Dec = Int32.Parse(GC_Temp, System.Globalization.NumberStyles.HexNumber);
                            Reg_IRC.IRC_GC_In[GC_set, RGB_Count, GC_Count] = GC_Dec;

                            GC_Count = GC_Count + 1;
                        }
                    }
                }
                //else
                //{
                //    continue;
                //}
            }
        }
        private void GC_Out()
        {
            string[] list;
            int i, j, DBV_set, GC_Count, RGB_Count, Regw_Flag;
            string line, CMD, GC_Hex, GC_H_Byte, GC_L_Byte;

            DBV_set = 0;
            GC_Count = 0;
            RGB_Count = 0;
            Regw_Flag = 0;//0:Have "Regw" 1:No "Regw"

            //CMD2P3
            richTextBox_GC_Out.Text += " F0 ,AA,13" + "\n\n";

            for (i = 0; i < Reg_IRC.IRC_Msg.Length; i++)
            {
                line = Reg_IRC.IRC_Msg[i];
                line = line.Replace(" ", "");
                list = line.Trim().Split(',');

                if (list[0] == "")
                {
                    richTextBox_GC_Out.Text += line + "\n";
                    continue;
                }
                //else if ((list[0] == "REGW") || (list[0] == "regw") || (list[0].Substring(0, 2) == "0x"))
                {
                    if (list[0].Substring(0, 2) == "0x")
                        Regw_Flag = 1;
                    else
                        Regw_Flag = 0;

                    CMD = list[1 - 1].Substring(0, 2);
                    if ((CMD == "B0") || (CMD == "B1") || (CMD == "B2") || (CMD == "B3"))
                    {
                        RGB_Count = 0;
                    }
                    else if ((CMD == "B4") || (CMD == "B5") || (CMD == "B6") || (CMD == "B7"))
                    {
                        RGB_Count = 1;
                    }
                    else if ((CMD == "B8") || (CMD == "B9") || (CMD == "BA") || (CMD == "BB"))
                    {
                        RGB_Count = 2;
                    }

                    if (CMD == "B0" || (CMD == "B1") || (CMD == "B2") || (CMD == "B4") || (CMD == "B5") || (CMD == "B6") || (CMD == "B8") || (CMD == "B9") || (CMD == "BA"))
                    {
                        richTextBox_GC_Out.Text += "" + CMD;

                        for (j = 0; j < 8; j++)
                        {
                            GC_Hex = Convert.ToString(Reg_Result_IRC.IRC_GC_Out[DBV_set, RGB_Count, GC_Count], 16);//To Hex
                            GC_Hex = GC_Hex.ToUpper().PadLeft(4, '0');//Add "0"
                            //GC_Hex = GC_Hex.ToUpper();
                            GC_H_Byte = " ," + GC_Hex.Substring(0, 2);
                            GC_L_Byte = " ," + GC_Hex.Substring(2, 2);

                            richTextBox_GC_Out.Text += GC_H_Byte + GC_L_Byte;
                            GC_Count = GC_Count + 1;
                        }
                    }
                    else if ((CMD == "B3") || (CMD == "B7") || (CMD == "BB"))
                    {
                        richTextBox_GC_Out.Text += " " + CMD;

                        GC_Hex = Convert.ToString(Reg_Result_IRC.IRC_GC_Out[DBV_set, RGB_Count, GC_Count], 16);//To Hex
                        GC_Hex = GC_Hex.ToUpper().PadLeft(4, '0');//Add "0"
                        //GC_Hex = GC_Hex.ToUpper();
                        GC_H_Byte = " ," + GC_Hex.Substring(0, 2);
                        GC_L_Byte = " ," + GC_Hex.Substring(2, 2);

                        richTextBox_GC_Out.Text += GC_H_Byte + GC_L_Byte;
                        GC_Count = 0;

                        if (CMD == "BB")
                            DBV_set = DBV_set + 1;
                    }
                    else
                    {
                        richTextBox_GC_Out.Text += line + "\n";
                        continue;
                    }
                    richTextBox_GC_Out.Text += "\n";

                }
                //else
                //{
                //    richTextBox_GC_Out.Text += line + "\n";
                //    continue;
                //}

            }
        }
        private void IRC_Reg_Out()
        {
            int i;
            int[] OPR_Dif = { 0, 0, 0, 0 };
            string[] CMD_D5 = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
            string[] CMD_D6 = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };
            string[] Para = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0" };

            //CMD2P1
            richTextBox_GC_Out.Text += " F0 , AA , 11" + "\n";
            //0xD5
            richTextBox_GC_Out.Text += "\n";
            richTextBox_GC_Out.Text += " D5 ";
            for (i = 0; i < 23; i++)
            {
                CMD_D5[i] = " ," + Convert.ToString(Reg_Result_IRC.Reg_Out[0, i], 16).ToUpper().PadLeft(2, '0');
                richTextBox_GC_Out.Text += CMD_D5[i];
            }

            //0xD6
            richTextBox_GC_Out.Text += "\n";
            richTextBox_GC_Out.Text += " D6 ";
            for (i = 0; i < 17; i++)
            {
                CMD_D6[i] = " ," + Convert.ToString(Reg_Result_IRC.Reg_Out[1, i], 16).ToUpper().PadLeft(2, '0');
                richTextBox_GC_Out.Text += CMD_D6[i];
            }
        }
        private void button_Run_IRC_Click(object sender, EventArgs e)
        {
            Class1 IRC = new Class1();
            labELVDDC.Text = ELVDDC + "mA";
            richTextBox_GC_Out.Clear();
            IRC_Reg_Ini();   //获取dll输入参数，包含Max_DBV ELVDD/ELVSS 电流值等参数、不同画面下的5组255灰阶对应的RGB寄存器值 以及DBV 分段参数值
            GC_In();  //获取普通模式下的Gamma寄存器值；
            IRC.IRC_In_Out(Reg_IRC.Para, Reg_IRC.Ratio_V255_GC, Reg_IRC.DBV, Reg_IRC.DBV_Mode, Reg_IRC.IRC_GC_In, ref Reg_Result_IRC.IRC_GC_Out, ref Reg_Result_IRC.Reg_Out);
            GC_Out(); // 输出修正后的Gamma
            IRC_Reg_Out();  //输出D5 D6 参数 及使能
        }
    }
}
